在 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
is_null($x) vs $x === null in PHP
提问by Explosion Pills
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.
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 === null
does 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_null
and === null
.
有绝对没有在两者之间的功能差异is_null
和=== null
。
The only difference is that is_null
is a function and thus
唯一的区别是它is_null
是一个函数,因此
- is marginally slower (function call overhead)
- can be used as a callback, e.g.
array_map('is_null', $array)
.
- 稍慢(函数调用开销)
- 可以用作回调,例如
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_NULL
case.
如果你愿意,你可以检查代码:is_identical_function
( ===
) 和php_is_type
( is_null
) 对IS_NULL
案例做同样的事情。
The related isset()
language construct checks whether the variable actually exists before doing the null
check. So isset($undefinedVar)
will not throw a notice.
相关isset()
语言构造在进行null
检查之前检查变量是否实际存在。所以isset($undefinedVar)
不会扔通知。
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
).
另请注意,即使值是isset()
有时也可能返回- 这是在重载对象上使用时的情况,即如果对象定义了/方法,即使偏移量也是返回(这实际上很常见,因为人们使用在/ )true
null
offsetExists
__isset
true
null
array_key_exists
offsetExists
__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 进行测试
回答by mAu
I'm not able to say wether it's better to use is_null
or === null
. But be aware when using isset
on 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_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
.
is_null
和之间没有真正的区别=== null
。我认为===
要好得多,但是当您例如call_user_func
出于某种可疑原因需要使用时,您必须使用is_null
.
回答by PiTheNumber
===
and is_null
is the same.
===
并且is_null
是一样的。
According to this commentis_null
is 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
。尤其是看评论区。