Understanding operator precedence in php

可紊 提交于 2019-12-19 18:26:36

问题


I have the following code in production that appears to be causing an infinite loop.

 $z=1;
 while (!$apns = $this->getApns($streamContext) && $z < 11)
 {
    myerror_log("unable to conncect to apple. sleep for 2 seconds and try again");
    $z++;
    sleep(2);
 }

How are the precedence rules getting applied that cause this behavior?

http://php.net/manual/en/language.operators.precedence.php

I see this note in the docs:

Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.

Which makes me think the the = should be evaluated first. then the ! then the &&, which would not cause an infinite loop.


回答1:


Your code is evaluating like this:

while (!($apns = ($this->getApns($streamContext) && ($z < 11))))

which is why you see the infinite loop (as soon as $z >= 11, $apns is false, so the condition is always true). The reason for this precedence is that the special rules only apply to ! on the left of the assignment being valid (having lower precedence than =). It has no effect on the boolean operator on the right, which behaves as it would in any sane language.

Your style is bad. Try this, which is much more readable and only differs in the final value of $z (and if that's important you can tweak the break statement.

for( $z = 1; $z < 11; ++ $z ) {
    // note extra brackets to make it clear that we intend to do assignment not comparison
    if( ($apns = $this->getApns($streamContext)) ) {
        break;
    }
    myerror_log("unable to conncect to apple. sleep for 2 seconds and try again");
    sleep(2);
}



回答2:


Your code is clear example of why it's good habit to always put all the conditions in brackets (and the same applies to code block. Even oneliners should be surrounded by { and }). So instead of error-prone:

while (!$apns = $this->getApns($streamContext) && $z < 11)

do

while (!($apns = $this->getApns($streamContext)) && ($z < 11))

and you will be safe.



来源:https://stackoverflow.com/questions/18519473/understanding-operator-precedence-in-php

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