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 === 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())方法来确定一个值是否为null: is_null()和== null。我已经听说了,但是没有得到确认,== null是更快的,但是在一个代码检查中,有人强烈建议我使用is_null(),因为它是专门为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,并且可能是首选。有什么理由使用它吗?是一个总是喜欢吗?收取()呢?
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?
作为可能不关闭这个问题的附录,isset()和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?关于零的东西没有可比性?
7 个解决方案
#1
145
There is absolutely no difference 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_idal_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
).
还要注意,有时收取()返回true即使值是null -这是一个重载的对象上使用的情况时,即如果该对象定义了一个offsetExists / __isset方法返回true即使抵消为空(这是很常见的,因为人们用array_key_exists offsetExists / __isset)。
#2
10
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
测试与= = = 0.0090668201446533
Testing with is_null() 0.013684034347534
0.013684034347534测试与is_null()
参见操作中的代码。
#3
4
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 >
#4
3
You need isset()
if the variable is possibly not defined. It returns false when the variable is not defined or === null
(yes, it's that ugly). Only isset()
and empty()
do not raise an E_NOTICE if the variable or array element does not exist.
如果变量可能没有定义,则需要isset()。当变量未被定义或== null时,它将返回false(是的,它是那么难看)。如果变量或数组元素不存在,则只有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。
#5
2
===
and is_null
is the same.
===和is_null是相同的。
According to this comment is_null
is only 250ns slower. I think because a function is slower than an operator.
根据这个注释,is_null的速度只有250ns。因为函数比运算符慢。
#6
2
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
#7
1
The PHP documentation has a good discussion and experiments on is_null, === null, isset
. Especially read the comment section.
PHP文档在is_null上有很好的讨论和实验,== null, isset。特别是阅读评论部分。
#1
145
There is absolutely no difference 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_idal_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
).
还要注意,有时收取()返回true即使值是null -这是一个重载的对象上使用的情况时,即如果该对象定义了一个offsetExists / __isset方法返回true即使抵消为空(这是很常见的,因为人们用array_key_exists offsetExists / __isset)。
#2
10
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
测试与= = = 0.0090668201446533
Testing with is_null() 0.013684034347534
0.013684034347534测试与is_null()
参见操作中的代码。
#3
4
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 >
#4
3
You need isset()
if the variable is possibly not defined. It returns false when the variable is not defined or === null
(yes, it's that ugly). Only isset()
and empty()
do not raise an E_NOTICE if the variable or array element does not exist.
如果变量可能没有定义,则需要isset()。当变量未被定义或== null时,它将返回false(是的,它是那么难看)。如果变量或数组元素不存在,则只有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。
#5
2
===
and is_null
is the same.
===和is_null是相同的。
According to this comment is_null
is only 250ns slower. I think because a function is slower than an operator.
根据这个注释,is_null的速度只有250ns。因为函数比运算符慢。
#6
2
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
#7
1
The PHP documentation has a good discussion and experiments on is_null, === null, isset
. Especially read the comment section.
PHP文档在is_null上有很好的讨论和实验,== null, isset。特别是阅读评论部分。