PHP is_null() 和 ==null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9671659/
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
PHP is_null() and ==null
提问by kel_ff0080
In PHP, what is the difference between is_nulland ==nullin PHP? What are the qualifications for both to return true?
在 PHP 中,is_null和==null在 PHP 中有什么区别?两者返回true的条件是什么?
回答by Rocket Hazmat
is_nullis the same as === null. Both return true when a variable is null(or unset).
is_null与 相同=== null。当变量是null(或未设置)时,两者都返回真。
Note that I'm using ===and not ==. ===compares type as well as value.
请注意,我正在使用===而不是==. ===比较类型和值。
回答by Daniel Ribeiro
So you can understand it better:
所以你可以更好地理解它:
$a = null;
$b = 0;
is_null($a) // TRUE
$a == null // TRUE
$a === null // TRUE
is_null($b) // FALSE
$b == null // TRUE
$b === null // FALSE
回答by keithhatfield
There are a couple really good charts on the php.net site that show how different values react:
php.net 站点上有几个非常好的图表,显示了不同值的反应:
回答by Umut KIRG?Z
You can check the comparison between is_null()and null === $var
您可以检查is_null()和null === $var之间的比较
回答by Akim Kelar
===nullis recommended by Rasmus Lerdorf, the inventor of PHP.
Rasmus says the test for null is faster than the test of isset. His recommendation is sufficient reason to look at the difference seriously. The difference would be significant if you had a small loop going through the same code thousands of times in the one Web page request.
===null由 PHP 的发明者 Rasmus Lerdorf 推荐。Rasmus 说 null 的测试比isset. 他的建议是认真看待差异的充分理由。如果您有一个小循环在一个 Web 页面请求中执行数千次相同的代码,则差异将非常显着。
UPD: Some speed test for is_null and strict comparison:
UPD: is_null 和严格比较的一些速度测试:
PHP 5.5.9
is_null - float(2.2381200790405)
=== - float(1.0024659633636)
PHP 7.0.0-dev
is_null - float(1.4121870994568)
=== - float(1.4577329158783)
回答by DanRedux
==doesn't check the type, so somehow, somewhere, something like the string ''or the string 'null'may come up as equal to null.
==不检查类型,所以不知何故,在某处,字符串''或字符串之类的东西'null'可能会等于 null。
Use triple equals sign, ===, to not only check two values are equal but also that they are of the same type.
使用三重等号 ,===不仅可以检查两个值是否相等,还可以检查它们是否属于同一类型。

