Understanding operator precedence in php

后端 未结 2 764
遥遥无期
遥遥无期 2021-01-18 15:18

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

 $z=1;
 while (!$apns = $this->getApns($streamContext) && $z          


        
2条回答
  •  春和景丽
    2021-01-18 15:51

    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.

提交回复
热议问题