问题
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