PHP Logical Operators precedence affects variable assignment results strangely

北慕城南 提交于 2019-12-02 09:08:30

PHP: Operator Precendence

&& has a higher precedence than =, so in the second if, you are assigning the value of fn1(1) && $var4 == 123 (true or false) to $var2.

In the first if, AND has a lower precedence than =, so the assignment happens first, then the result is compared.

In the third if, the assignment happens first again because everything in parens gets processed first.

&& has a higher precedence than =, so what's really happening is something more like:

if ($var1 = (fn(1) && $var4 == 123))

So what is really being assigned to $var1 is the boolean result, which is why you get 1.

PHP's AND and && operators both are logical ands, but the and version has a lower binding precedence, see: http://php.net/manual/en/language.operators.precedence.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!