PHP is_null() Function
The PHP is_null() function checks whether a variable is null. The function returns true if the variable is null, otherwise it returns false.
Syntax
is_null(variable)
Parameters
variable |
Required. Specify the variable being evaluated. |
Return Value
Returns true if variable is null, false otherwise.
Example:
The example below shows the usage of is_null() function.
<?php var_dump(is_null(null)); //returns: bool(true) var_dump(is_null(NULL)); //returns: bool(true) var_dump(is_null($xyz)); //returns: bool(true) echo "\n"; var_dump(is_null(10)); //returns: bool(false) var_dump(is_null(10.5)); //returns: bool(false) var_dump(is_null(1e5)); //returns: bool(false) var_dump(is_null('xyz')); //returns: bool(false) var_dump(is_null(true)); //returns: bool(false) var_dump(is_null(array())); //returns: bool(false) ?>
The output of the above code will be:
bool(true) bool(true) bool(true) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) PHP Warning: Undefined variable $xyz in Main.php on line 4
Example:
Consider one more example where this function is used to check all elements of an array if they are null or not.
<?php $Arr = array($x, 10, 10.5, null, false, NULL, "xyz", 1e3, array()); foreach ($Arr as $value) { echo "is_null(".var_export($value, true).") = "; var_dump(is_null($value)); } ?>
The output of the above code will be:
is_null(NULL) = bool(true) is_null(10) = bool(false) is_null(10.5) = bool(false) is_null(NULL) = bool(true) is_null(false) = bool(false) is_null(NULL) = bool(true) is_null('xyz') = bool(false) is_null(1000.0) = bool(false) is_null(array ( )) = bool(false) PHP Warning: Undefined variable $x in Main.php on line 2
❮ PHP Variable Handling Reference