NULL Concept
NULL is a data type that indicates the absence of a value.
It has only one possible value: the null value.
A variable also has the NULL type when it is explicitly assigned the null value.
null value is the only value of the NULL type.
A variable with the NULL type always has the case-insensitive constant null as its value.
When a variable is assigned the null value, that variable has the NULL type.
The concepts of the NULL type and the null value can be summarized as follows:
The NULL type
is a data type that indicates the absence of a value.
A variable also has the NULL type when it is explicitly assigned the null value. The NULL type has only one value: the case-insensitive constant null.
Both the NULL type and the null value are case-insensitive.
The NULL type is equivalent to the Null and null types, and the null value is equivalent to the Null and NULL values.
In this document, the type and the value are distinguished for clarity by referring to the type as "NULL type" and the value as the null value, and this convention will be used consistently throughout.
Example
/* Variable $a with no assigned value (Undefined state) */
$a;
var_dump($a); // NULL (Warning is generated)
/* When a variable is explicitly assigned the null value */
$b = null; // Case-insensitive
var_dump($b); // NULL
$c = Null; // Case-insensitive
var_dump($c); // NULL
$d = NULL; // Case-insensitive
var_dump($d); // NULL
/* Variable $e that has not been declared (Undefined state) */
var_dump($e); // NULL (Warning is generated)
Warning!
Using either a variable with no assigned value or an undeclared variable will generate a Warning: Undefined variable.
Checking the NULL Type
gettype() Function
The gettype() function returns the data type of the given value as a string.
The following example demonstrates this.
$a = null;
$b; // Variable $b with no assigned value (Undefined state)
echo gettype($a); // 'NULL'
echo gettype($b); // 'NULL' (Warning is generated)
is_null() Function
The is_null() function checks whether a given value is NULL.
It returns true if the value passed as an argument is NULL; otherwise, it returns false.
The following example demonstrates this.
$a = null;
$b; // Variable $b with no assigned value (Undefined state)
var_dump(is_null($a)); // true
var_dump(is_null($b)); // true (Warning is generated)
Converting to the NULL Type
To convert a variable to the NULL type, you only need to assign the null value to the variable.
/* Assigning null as the initial value to make it the NULL type */
$a = null;
var_dump($a); // NULL
/* Converting a variable of string type to the NULL type */
$b = "val";
$b = null;
var_dump($b); // NULL
Caution
Do not use unset() or type juggling such as (unset) $var in an attempt to make a variable NULL.
Using unset()
unset() is used to unset (remove) a variable. However, using this language construct to convert a variable to the NULL type may not be safe. This is because unset() removes the reference to the variable. Converting a variable to the NULL type simply indicates that the value is absent, whereas using unset() removes the variable itself.
In addition, depending on PHP settings, accessing a variable after using unset() may generate a Warning: Undefined variable. This occurs because the variable no longer exists when accessed.
$var = "str";
unset($var);
// Notice: Undefined variable: f in.....
var_dump($var); // NULL
$var = "str";
$var = null;
var_dump($var); // NULL
Using Type Juggling Such as (unset) $var
This feature has been deprecated since PHP 7.2.0 and was completely removed as of PHP 8.0.0, so relying on this behavior is strongly discouraged.
$var = "str";
var_dump((unset)$var); // NULL
$var = "str";
$var = null;
var_dump($var); // NULL
To convert a variable to the NULL type, you only need to assign the null value to the variable.
Evaluating the NULL Type as Boolean false
When evaluated as a boolean type, the NULL type resolves to false.
$var = null;
if ($var) {
echo 'Evaluated as true.';
} else {
echo 'Evaluated as false.';
}
// Output: 'Evaluated as false.'
var_dump(is_bool($var)); // bool(false)
Distinguishing the NULL Type from Empty Strings and Empty Arrays
Empty strings and empty arrays are not the NULL type.
$e = '';
$f = array();
var_dump(is_null($e)); // bool(false)
var_dump(is_null($f)); // bool(false)