is_null($ x)vs $ x ===在PHP中为null [英] is_null($x) vs $x === null in PHP

查看:109
本文介绍了is_null($ x)vs $ x ===在PHP中为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
is_null($ var)和($ var = ==空)?

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

PHP有两个(据我所知,如果您计算isset()则为三个)方法来确定值是否为空:is_null()=== null.我听说过但未确认=== null更快,但是在代码审查中,有人强烈建议我使用is_null()代替,因为它是专门为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.

无论如何,is_null()显然更慢的事实也使我相信它的作用比=== null还要多,并且可能是首选.有任何理由使用其中之一吗?总是首选吗?那isset()呢?

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

作为可能无法解决此问题的附录,isset()is_null()呢?似乎所有isset()都将取消显示通知,因此,除非您实际上想要未定义变量的通知,否则有理由使用is_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?

最后,有什么数学理由更喜欢is_null()而不是=== null吗?关于null无法比较的东西?

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

推荐答案

is_null=== null之间的功能上没有 完全没有 .

There is absolutely no difference in functionality between is_null and === null.

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

The only difference is that is_null is a function and thus

  1. 稍微慢一点(函数调用开销)
  2. 可用作回调,例如array_map('is_null', $array).

我个人会尽可能使用null ===,因为它与false ===true ===检查更加一致.

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

如果需要,可以检查代码: is_identical_function (===)和 php_is_type (is_null)对于IS_NULL情况也做同样的事情.

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.

相关的isset()语言构造在进行null检查之前检查变量是否确实存在.因此isset($undefinedVar)不会发出通知.

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

还请注意,即使值是nullisset()有时也可能返回true-这种情况适用于重载对象,即,如果对象定义了offsetExists/__isset即使偏移量为null的方法也返回true(这实际上很常见,因为人们在offsetExists/__isset中使用array_key_exists).

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).

这篇关于is_null($ x)vs $ x ===在PHP中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆