变量===值和值===变量之间的区别?

发布于 2024-12-07 18:51:09 字数 303 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜清冷一曲。 2024-12-14 18:51:09

不,没有区别。

当您编写 $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.

冰葑 2024-12-14 18:51:09

没有区别,它用于避免错误(例如将变量设置为 null,而不是比较它们),但是 null === $object 通常被认为是坏的避免拼写错误的方法(c)。

There is no difference, it is used to avoid mistakes (like setting variable to null, not comparing them), however the null === $object is often considered the Bad Way (c) to avoid typos.

瑾兮 2024-12-14 18:51:09

$object === null 表达式比 null === $object 更加人性化,因为第二个表达式打破了从左到右的自然阅读顺序。这就是为什么即使对于解释器来说没有太大区别,但人类阅读起来却有点困难。涉及一些逻辑 - 如果你使用 if..else 语句,它听起来应该是什么样子? “如果null等于$object..等一下,nullnull,它怎么可能等于还有什么吗?哦,天哪,我们实际上将右手值与左手值进行比较,这是相反的东西。所以,如果 $object 等于 null 那么我们应该.. ”。而你每次都会这样想。

我的结论是:尽可能使用 $value == const!很久以前,人们编写了 if ($value = const) 但这些时代已经过去了。现在每个 IDE 都可以告诉你这些简单的错误。

The $object === null expression is much more human-friendly then null === $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? "If null equals $object.. Wait a minute, null is null, 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 equals null then we should..". And your think this way every time.

My conclusion is: use $value == const every time you can! Long time ago people wrote if ($value = const) but these times have passed. Now every IDE can tell ya about such simple errors.

掌心的温暖 2024-12-14 18:51:09

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.

路弥 2024-12-14 18:51:09

这是开发人员尝试阻止意外赋值的选择。

两种比较方法之间的功能完全相同,但在“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.

晨曦÷微暖 2024-12-14 18:51:09

第二种方法检查变量的值和类型是否为 null,两种方法中的错误应该不同。

The second method checks the value and type of variable against null, The errors should be different in tow methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文