参见英文答案 > Why does PHP evaluate $b and $b = $b differently when used with $b++ in array index 1个
我在SoloLearn上玩挑战,我发现了这个:
var x = 5;
document.write(x * x++);
// will output 25
但如果我们在PHP中编写相同的东西:
$x = 5;
echo $x * $x++;
// it will output 30!
这是件事,不是吗?
那么普通程序员怎么能向他的奶奶解释这个呢?
解决方法:
原因是PHP中的Undefined order of evaluation:
Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation. PHP does not (in the general case) specify in which order an expression is evaluated and code that assumes a specific order of evaluation should be avoided, because the behavior can change between versions of PHP or depending on the surrounding code.