PHP Post and preincrement

微笑、不失礼 提交于 2020-01-03 17:26:37

问题


I have found in PHP some strange calculation, for example this:

$c=5;

$r = $c + ($c++ + ++$c);

echo $r;

Why result is 19 and not 17?

Thanks


回答1:


The result should be unspecified. Please read the following PHP specification:
https://github.com/php/php-langspec/blob/master/spec/10-expressions.md

While precedence, associativity, and grouping parentheses control the order in which operators are applied, they do not control the order of evaluation of the terms themselves. Unless stated explicitly in this specification, the order in which the operands in an expression are evaluated relative to each other is unspecified. See the discussion above about the operators that contain sequence points. (For example, in the full expression $list1[$i] = $list2[$i++], whether the value of $i on the left-hand side is the old or new $i, is unspecified. Similarly, in the full expression $j = $i + $i++, whether the value of $i is the old or new $i, is unspecified. Finally, in the full expression f() + g() * h(), the order in which the three functions are called, is unspecified).

You could find the same reasoning in PHP documentation too:



来源:https://stackoverflow.com/questions/46909693/php-post-and-preincrement

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