IF语句中值之前或之后的变量 [英] Variable before or after value in IF statement

查看:146
本文介绍了IF语句中值之前或之后的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个陈述之间是否有区别:



if($ a =='hello'){...}





if('hello'== $ a){...}



我注意到像Wordpress这样的应用程序倾向于使用后者,而我通常使用前者。 / p>

我似乎记得刚才读一些东西为后者辩护,但我不记得其背后的原因。

解决方案

没有区别。使用它是为了在你不小心进行作业而不是比较时抛出错误:

  if('hello' = $ a)

if($ a ='hello')将分配给变量而不会抛出错误。



它也可能用于明确显示 if 你有在 if 语句中的赋值,你真的想要它在那里并且它不是一个错误(一致性)。


Is there a difference between these two statements:

if ($a == 'hello') { ... }

and

if ('hello' == $a) { ... }

I've noticed applications like Wordpress tend to use the latter, whereas I usually use the former.

I seem to remember reading something a while ago giving justification for the latter, but I can't recall the reasoning behind it.

解决方案

There is no difference. It is used so that an error is thrown in case you accidentally make an assignment instead of a comparison:

if('hello' = $a)

if($a = 'hello') would assign to the variable and not throw an error.

It might also be used to explicitly show that if you have an assignment in an if statement, you really want to have it there and it is not a mistake (consistency).

这篇关于IF语句中值之前或之后的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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