在 PHP 中 is_null($x) 与 $x === null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8228837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:13:02  来源:igfitidea点击:

is_null($x) vs $x === null in PHP

phpisnull

提问by Explosion Pills

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

可能的重复:
is_null($var) 和 ($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 === nullis 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.

PHP 有两个(我知道,如果你算的话,三个isset())方法来确定一个值是否为空:is_null()=== null。我听说过,但没有证实,这=== null更快,但在代码中,有人强烈建议我改用is_null()它,因为它是专为空评估目的而设计的。他也开始谈论数学什么的。

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

无论如何,is_null()显然更慢的事实也让我相信它做得更多=== null并且可能是首选。有什么理由使用其中之一吗?总是首选吗?怎么样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 wanta notice for an undefined variable, any reason to use is_null()instead? How about if you know the variable is initialized at the time?

作为可能无法解决此问题的附录,isset()vs.is_null()呢?似乎所有isset()要做的就是取消通知,所以除非你真的想要一个未定义变量的通知,否则有什么理由is_null()改用?如果您知道该变量当时已初始化呢?

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

最后,是否有任何数学理由,更喜欢is_null()=== null?关于 null 的东西没有可比性?

回答by NikiC

There is absolutely nodifference in functionality between is_nulland === null.

绝对没有在两者之间的功能差异is_null=== null

The only difference is that is_nullis a function and thus

唯一的区别是它is_null是一个函数,因此

  1. is marginally slower (function call overhead)
  2. can be used as a callback, e.g. array_map('is_null', $array).
  1. 稍慢(函数调用开销)
  2. 可以用作回调,例如array_map('is_null', $array)

Personally, I use null ===whenever I can, as it is more consistent with false ===and true ===checks.

就个人而言,我null ===尽可能使用,因为它更符合false ===true ===检查。

If you want, you can check the code: is_identical_function(===) and php_is_type(is_null) do the same thing for the IS_NULLcase.

如果你愿意,你可以检查代码:is_identical_function( ===) 和php_is_type( is_null) 对IS_NULL案例做同样的事情。



The related isset()language construct checks whether the variable actually exists before doing the nullcheck. So isset($undefinedVar)will not throw a notice.

相关isset()语言构造在进行null检查之前检查变量是否实际存在。所以isset($undefinedVar)不会扔通知。

Also note that isset()may sometimes return trueeven 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/__issetmethod that returns trueeven if the offset is null(this is actually quite common, because people use array_key_existsin offsetExists/__isset).

另请注意,即使值是isset()有时也可能返回- 这是在重载对象上使用时的情况,即如果对象定义了/方法,即使偏移量也是返回(这实际上很常见,因为人们使用在/ )truenulloffsetExists__issettruenullarray_key_existsoffsetExists__isset

回答by jprofitt

As stated by others, there is a time difference between using ===and is_null(). Did some quick testing and got these results:

正如其他人所说,使用===和之间存在时间差异is_null()。做了一些快速测试,得到了这些结果:

<?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

使用 === 0.0090668201446533 进行测试

使用 is_null() 0.013684034347534 进行测试

See the code in action

查看运行中的代码

回答by mAu

I'm not able to say wether it's better to use is_nullor === null. But be aware when using isseton arrays.

我不能说使用is_null或更好=== null。但是isset在数组上使用时要注意。

$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

回答by Marc B

They all have their places, though only isset() will avoid undefined variable warnings:

它们都有自己的位置,但只有 isset() 会避免未定义的变量警告:

$ 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 >

回答by ThiefMaster

You need isset()if the variable is possibly not defined. It returns false when the variable is not defined or === null(yes, it's thatugly). Only isset()and empty()do not raise an E_NOTICE if the variable or array element does not exist.

您需要isset()如果变量可能是没有定义。当没有定义的变量或返回false === null(是的,这丑陋的)。只有isset()empty()如果变量或数组元素不存在不引发E_NOTICE。

There is not really a difference between is_nulland === null. I think ===is much nicer but when you e.g. need to use call_user_funcfor some dubious reason, you'd have to use is_null.

is_null和之间没有真正的区别=== null。我认为===要好得多,但是当您例如call_user_func出于某种可疑原因需要使用时,您必须使用is_null.

回答by PiTheNumber

===and is_nullis the same.

===并且is_null是一样的。

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

根据此评论is_null仅慢 250ns。我认为是因为函数比运算符慢。

回答by Shakti Singh

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

PHP文档有一个很好的讨论和实验is_null, === null, isset。尤其是看评论区。