Current Location: Home> Latest Articles> Comprehensive Guide to PHP Null Checking: Detailed Explanation of isset, empty, and is_null Functions

Comprehensive Guide to PHP Null Checking: Detailed Explanation of isset, empty, and is_null Functions

gitbox 2025-06-24

1. Concept of Null

In computer science, null refers to the state where a variable or object does not have a value. In PHP, when a variable is unassigned, its default value is NULL. Null (NULL) is a special type in PHP used to indicate a variable has no value.

2. Null Checking Functions

2.1 isset()

The isset() function checks whether a variable has been set and is not NULL. If the variable exists and is not null, it returns true; otherwise, false.


$a = 10;
if (isset($a)) {
    echo '$a is set';
} else {
    echo '$a is not set';
}
// Output: $a is set

2.2 empty()

The empty() function checks whether a variable is empty. It returns TRUE if the variable does not exist or its value is one of the following: "", 0, "0", NULL, FALSE, or an empty array; otherwise, it returns FALSE.


$a = null;
if (empty($a)) {
    echo '$a is empty';
} else {
    echo '$a is not empty';
}
// Output: $a is empty

2.3 is_null()

The is_null() function tests whether a variable is NULL. It returns TRUE if the variable's value is NULL; otherwise, FALSE.


$a = null;
if (is_null($a)) {
    echo 'The variable is null';
} else {
    echo 'The variable is not null';
}
// Output: The variable is null

3. Comprehensive Example


$name = '';

if (isset($name)) {
    echo '$name is set';
} else {
    echo '$name is not set';
}
// Output: $name is set

if (empty($name)) {
    echo '$name is empty';
} else {
    echo '$name is not empty';
}
// Output: $name is empty

if (is_null($name)) {
    echo 'The variable is null';
} else {
    echo 'The variable is not null';
}
// Output: The variable is not null

In this example, the variable $name is assigned an empty string. The isset() function returns true, indicating the variable is set. The empty() function returns true, indicating the variable is empty. The is_null() function returns false, indicating the variable is not null but an empty string.