dongqian5384
dongqian5384
2013-08-29 19:27

理解php中的运算符优先级

  • operators
  • php

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.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐