$x === nullNull is a special data type in PHP which can have only one value that is NULL. A variable of data type NULL is a variable that has no value assigned to it. Any variable can be empty by setting the value NULL to the variable.
The logical implications of Null are often different - in some languages NULL is not equal to anything, so if(a == NULL) will always be false.
The is_null() function checks whether a variable is NULL or not. This function returns true (1) if the variable is NULL, otherwise it returns false/nothing.
To check if the variable is NULL, use the PHP is_null() function. The is_null() function is used to test whether the variable is NULL or not. The is_null() function returns TRUE if the variable is null, FALSE otherwise. There is only one value of type NULL, and it is the case-insensitive constant NULL.
There is absolutely no difference in functionality between is_null
and === null
.
The only difference is that is_null
is a function and thus
array_map('is_null', $array)
.Personally, I use null ===
whenever I can, as it is more consistent with false ===
and true ===
checks.
If you want, you can check the code: is_identical_function
(===
) and php_is_type
(is_null
) do the same thing for the IS_NULL
case.
The related isset()
language construct checks whether the variable actually exists before doing the null
check. So isset($undefinedVar)
will not throw a notice.
Also note that isset()
may sometimes return true
even though the value is null
- this is the case when it is used on an overloaded object, i.e. if the object defines an offsetExists
/__isset
method that returns true
even if the offset is null
(this is actually quite common, because people use array_key_exists
in offsetExists
/__isset
).
As stated by others, there is a time difference between using ===
and is_null()
. Did some quick testing and got these results:
<?php
//checking with ===
$a = array();
$time = microtime(true);
for($i=0;$i<10000;$i++) {
if($a[$i] === null) {
//do nothing
}
}
echo 'Testing with === ', microtime(true) - $time, "\n";
//checking with is_null()
$time = microtime(true);
for($i=0;$i<10000;$i++) {
if(is_null($a[$i])) {
//do nothing
}
}
echo 'Testing with is_null() ', microtime(true) - $time;
?>
Gives the results
Testing with === 0.0090668201446533
Testing with is_null() 0.013684034347534
See the code in action
They all have their places, though only isset() will avoid undefined variable warnings:
$ php -a
Interactive shell
php > var_dump(is_null($a));
PHP Notice: Undefined variable: a in php shell code on line 1
bool(true)
php > var_dump($a === null);
PHP Notice: Undefined variable: a in php shell code on line 1
bool(true)
php > var_dump(isset($a));
bool(false)
php >
I'm not able to say wether it's better to use is_null
or === null
. But be aware when using isset
on arrays.
$a = array('foo' => null);
var_dump(isset($a['foo'])); // false
var_dump(is_null($a['foo'])); // true
var_dump(array_key_exists('foo', $a)); // true
You need isset()
if the variable is possibly not defined. It returns false when the variable is not defined or === null
(yes, it's that ugly). Only isset()
and empty()
do not raise an E_NOTICE if the variable or array element does not exist.
There is not really a difference between is_null
and === null
. I think ===
is much nicer but when you e.g. need to use call_user_func
for some dubious reason, you'd have to use is_null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With