PHP比较中的可变位置 [英] Variable position in comparision in PHP
本文介绍了PHP比较中的可变位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下2种情况下哪个更优化?
Which is more optimized between 2 case below?
if ($var == 'value') {}
和
if ('value' == $var) {}
对不起,如果与另一个问题重复,但是我无法用谷歌搜索出答案.
Sorry if this is duplicated with another question but I can not google out the answer.
谢谢
[更新]
This's called Yoda Conditions, more information here.
推荐答案
没有实际区别.第二个用来保护自己免受错字if ($var = 'value')
的侵害,但不是很可读.除非您很累,以至于打字时会错过任何字符,否则请尽量使用第一个.
There's no actual difference. The second one is used to defend yourself from typo if ($var = 'value')
But not really readable. Use mostly the first one unless you are so tired that while typing you miss characters.
如果您编写代码
if ($var = 'val') echo $var; //Output will be "val"
但如果您这样做
if ('val' = $var) echo $var;
您会收到语法错误.
这篇关于PHP比较中的可变位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文