is_null($ var)和($ var === null)有什么区别? [英] What's the difference between is_null($var) and ($var === null)?
问题描述
这之间有什么区别吗...
Is there any difference between this...
if (is_null($var)) {
do_something();
}
还有这个?
if ($var === null) {
do_something();
}
检查变量是否包含null时,哪种格式更好?我应该注意哪些极端情况? (我初始化了所有变量,因此不存在的变量不是问题.)
Which form is better when checking whether or not a variable contains null? Are there any edge cases I should be aware of? (I initialize all my variables, so nonexistent variables are not a problem.)
推荐答案
提供了变量已初始化(您确实已指出-尽管我不确定在此情况下这是否重要,但100%不确定.两种解决方案都可能会抛出一个警告(如果未定义变量),它们在功能上是相同的.我认为===
会稍微快一些,尽管它消除了函数调用的开销.
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.
===
用于严格的数据比较. NULL只有一个值",因此可用于与NULL(这是null值"的PHP常量)进行比较
===
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
正在检查该变量是否为NULL数据类型.
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)和($ var === null)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!