PHP - if语句中的颠倒顺序 [英] PHP - reversed order in if statement

查看:97
本文介绍了PHP - if语句中的颠倒顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个问题,这让我很长时间,并找不到任何答案...
注意到它被很多Zend Framework开发人员使用,

This is a question that is bugging me for a long time and can't find any answer... Noticed it's used quite a lot by Zend Framework Developers,

以下2个if语句有什么区别? :

What is the difference between following 2 "if" statements? :

if (null === $this->user) { ... }

if ($this->user === null) { ... }

第一个看起来很奇怪;]

To me the first one looks kinda odd ;]

感谢回答。

推荐答案

p>这不是你的脚本工作方式的区别,它只是一个编码标准,一个建议

This is not a difference for the way your script works, it's just a coding standard, a recommendation

推荐使用它的原因是这样的: / p>

The reason why it is recommended to use it this way:

if (null == $this->user)

是这样的事实,如果你输入错误,而不是==,你会得到一个错误,而

is the fact that if you mistype and write = instead of == you will get an error, while

($this->user = null)

($this->user == null)

有效,但会导致奇怪的错误(赋值和最终值被计算为bool而不是比较)

works but causes weird bugs (assignment and the final value is evaluated as bool instead of comparison)

猜测它只是作为一个习惯扩展为严格的比较运算符(===)

and I guess it just extended as a habit to the strict comparison operator (===)

更新:因为我看到还有一些活动这个线程甚至3年后我发布的答案,我想我会添加一些我忘了提到。这种类型的符号称为 yoda条件,您可以在此维基百科页面

Update: since I see that there's still some activity on this thread even 3 years after I posted the answer I figured I would add something I forgot to mention. This type of notation is known as yoda conditions, you can read more about it on this wikipedia page for example.

这篇关于PHP - if语句中的颠倒顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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