Is_null ($var)和($var = = null)之间的区别是什么?

这有什么区别吗。

if (is_null($var)) {
do_something();
}

这个呢?

if ($var === null) {
do_something();
}

检查变量是否包含 null 时,哪种形式更好?有什么特殊情况需要我注意吗?(我初始化了所有的变量,所以不存在变量不是问题。)

7838 次浏览

Both are exactly same, I use is_null because it makes my code more readable

I would use the built in PHP function over the operator comparison every time.

Provided the variable is initialized (which you did indicate - though I'm not 100% sure if this matters in this context or not. Both solutions might throw a warning if the variable wasn't defined), they are functionally the same. I presume === would be marginally faster though as it removes the overhead of a function call.

It really depends on how you look at your condition.

=== is for a strict data comparison. NULL has only one 'value', so this works for comparing against NULL (which is a PHP constant of the null 'value')

is_null is checking that the variable is of the NULL data type.

It's up to you which you choose, really.

is_null($var) is about 14 times slower than $var===null... 37.8 ms vs. 2.6 ms.

But actually I don't know why.

I've just run a quick benchmark, testing a million iterations of each. is_null took 8 seconds to complete; === null took 1.

So a call to is_null is 0.000007s slower than a call to === on my computer.

I'd find something more useful to optimise.


My code:

<?php


$start = time();
$var = null;


for ($i = 1000000; $i--; ) {
is_null($var);
}


echo time() - $start;


$start = time();


for ($i = 1000000; $i--; ) {
$var === null;
}


echo time() - $start;

If it seems redundant for php to have so many is_foo() type functions, when you can just use a standard comparison operators, consider programatically called functions.

$arrayOfNullValues = array_filter($myArray, 'is_null');

empty() and isset() do not trigger a PHP warning if their parameter is an undefined variable. In most cases, such a warning is desirable to pinpoint the bugs. So only use the functions if you believe your variable can be legitimately undefined. It normally happens with an array index.

is true

is false

        |  isset   | is_null | === null | == null | empty   |
|-------|----------|---------|----------|---------|---------|
| unset |    ❌   |    ✅   |    ✅    |    ✅  |    ✅   |
|  null |    ❌   |    ✅   |    ✅    |    ✅  |    ✅   |
|  true |    ✅   |    ❌   |    ❌    |    ❌  |    ❌   |
| false |    ✅   |    ❌   |    ❌    |    ✅  |    ✅   |
|     0 |    ✅   |    ❌   |    ❌    |    ✅  |    ✅   |
|     1 |    ✅   |    ❌   |    ❌    |    ❌  |    ❌   |
|    \0 |    ✅   |    ❌   |    ❌    |    ❌  |    ❌   |
|    "" |    ✅   |    ❌   |    ❌    |    ✅  |    ✅   |
|    [] |    ✅   |    ❌   |    ❌    |    ✅  |    ✅   |

Summary:🔸♦️🔸

  • empty is equivalent to == null
  • is_null is equivalent to === null
  • isset is inverse of is_null and === null

One thing people often forget to mention in this discussion is that if you are all about strict type checking, is_null will help you to never make a typo in your comparison operators (== vs ===).