logical vs assignment operator precedence in PHP

后端 未结 1 393
生来不讨喜
生来不讨喜 2021-01-14 00:28

Recently i came upon such snippet:

$x = 2 && $y = 3; echo (int)$x.\':\'.(int)$y;

Which produces output 1:3. By looking a

相关标签:
1条回答
  • 2021-01-14 01:14

    The operator precedence sheet you link to states as a separate note:

    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.

    So, in effect, an assignment inside an expression will be treated somewhat like a sub-expression. Exactly how and when this will happen isn't clear from the documentation, which just states that "similar" expressions will work this way.

    0 讨论(0)
提交回复
热议问题