变量===值和值===变量之间的区别?
a)
if(null === $object)
{
//take some action
}
b)
if($object === null)
{
//take some action
}
我习惯于像 b) 那样做,但是在 Zend-Framework 中我发现他们到处都像 a) 那样做。它有什么好处吗??
谢谢。
a)
if(null === $object)
{
//take some action
}
b)
if($object === null)
{
//take some action
}
I am in habit of doing like b) but in Zend-Framework I find everywhere they have done it like a) . Is there any benefits of it ??
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,没有区别。
当您编写
$a = null
而不是$a == null
(或$a === null< /代码>)。在第一种情况下,您会得到逻辑错误,因为是赋值而不是比较,在第二种情况下,您会得到致命错误,这将帮助您更快地找到问题。
No, there is no difference.
The latter is supposed to help to avoid silly typos when you write
$a = null
instead of$a == null
(or$a === null
). In first case you'll get logical error, because of assignment instead of comparison, in second case - you'll get fatal error which will help you to find an issue sooner.没有区别,它用于避免错误(例如将变量设置为
null
,而不是比较它们),但是null === $object
通常被认为是坏的避免拼写错误的方法(c)。There is no difference, it is used to avoid mistakes (like setting variable to
null
, not comparing them), however thenull === $object
is often considered the Bad Way (c) to avoid typos.$object === null
表达式比null === $object
更加人性化,因为第二个表达式打破了从左到右的自然阅读顺序。这就是为什么即使对于解释器来说没有太大区别,但人类阅读起来却有点困难。涉及一些逻辑 - 如果你使用 if..else 语句,它听起来应该是什么样子? “如果null
等于$object
..等一下,null
是null
,它怎么可能等于还有什么吗?哦,天哪,我们实际上将右手值与左手值进行比较,这是相反的东西。所以,如果$object
等于null
那么我们应该.. ”。而你每次都会这样想。我的结论是:尽可能使用
$value == const
!很久以前,人们编写了if ($value = const)
但这些时代已经过去了。现在每个 IDE 都可以告诉你这些简单的错误。The
$object === null
expression is much more human-friendly thennull === $object
'cause second one breaks nature reading order which is left-to-right. That's why even if there is no much difference for interpreter but it's a bit harder to read by a human. Involving some logic - if you use if..else statement how it should sounds like? "Ifnull
equals$object
.. Wait a minute,null
isnull
, how can it be equal to something else? Oh, Gee, we actually comparing right-handed value to left-handed one, it's reversed stuff. So, if$object
equalsnull
then we should..". And your think this way every time.My conclusion is: use
$value == const
every time you can! Long time ago people wroteif ($value = const)
but these times have passed. Now every IDE can tell ya about such simple errors.b) 比 a) 更具可读性,
并且 a) 被一些过于谨慎的人认为不太容易出错,因为可能会将
==
与=
混淆>.但如果有三个=,我怀疑有人会把它与一个混淆。
b) is way more readable than a)
And a) is considered by some overcautious people as less error prone because of possible confusing
==
with=
.But in case of three ='s I doubt anyone will confuse it with one.
这是开发人员尝试阻止意外赋值的选择。
两种比较方法之间的功能完全相同,但在“a”的情况下,它会停止任何意外的值分配,因为您不能将某些内容分配给 null。
This a choice by the developer to attempt to stop accidential assignment of values.
The functionality is exactly the same between the two methods of comparison but in the case of "a" it stops any accidential assignment of values as you cannot assign something to null.
第二种方法检查变量的值和类型是否为 null,两种方法中的错误应该不同。
The second method checks the value and type of variable against
null
, The errors should be different in tow methods.