is_null($x) vs $x === null in PHP [duplicate]

2019-01-13 00:12发布

Possible Duplicate:
What's the difference between is_null($var) and ($var === null)?

PHP has two (that I know of, and three if you count isset()) methods to determine if a value is null: is_null() and === null. I have heard, but not confirmed, that === null is faster, but in a code review someone strongly suggested that I use is_null() instead as it is specifically designed for the null-evaluation purpose. He also started talking about math or something.

Anyway, the fact that is_null() is apparently slower also leads me to believe that it's doing more than === null does and is probably preferred. Is there any reason to use one or the other? Is one always preferred? What about isset()?

As an addendum to possibly not get this question closed, what about isset() vs. is_null()? It seems that all isset() will do is suppress the notice, so unless you actually want a notice for an undefined variable, any reason to use is_null() instead? How about if you know the variable is initialized at the time?

Finally, is there any mathematical reason to prefer is_null() over === null? Something about null not being comparable?

标签: php isnull
7条回答
霸刀☆藐视天下
2楼-- · 2019-01-13 00:44

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
查看更多
成全新的幸福
3楼-- · 2019-01-13 00:45

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 >
查看更多
够拽才男人
4楼-- · 2019-01-13 00:48

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

查看更多
在下西门庆
5楼-- · 2019-01-13 00:48

=== and is_null is the same.

According to this comment is_null is only 250ns slower. I think because a function is slower than an operator.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-13 01:01

The PHP documentation has a good discussion and experiments on is_null, === null, isset. Especially read the comment section.

查看更多
我命由我不由天
7楼-- · 2019-01-13 01:04

There is absolutely no difference in functionality between is_null and === null.

The only difference is that is_null is a function and thus

  1. is marginally slower (function call overhead)
  2. can be used as a callback, e.g. 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).

查看更多
登录 后发表回答