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.
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
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
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
$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.