有人可以帮我理解这段代码,因为它似乎不遵循 PHP 中递增/递减运算符的优先级和关联性原则:(这是来自 PHP 手动递增/递减运算符页面中的评论 - http:/ /php.net/manual/en/language.operators.increment.php )
第一个例子 -
$a = [ 0, 1, 2 ];
$i = 0;
$a[$i++] = $i;
var_dump( $a );
/* 这里是输出:
array (size=3)
0 => int 1
1 => int 1
2 => int 2
这是我对正在发生的事情的解释:
1. Array index gets calculated, so $a[$i++] is $a[0]
2. Then rval gets calculated (which after $i++ in the step above) is now 1
3. The value of the expression gets calculated which is 1.
到目前为止,一切都很好。*/
第二个例子 -
$a = [ 0, 1, 2 ];
$i = 0;
$a[$i] = $i++;
var_dump( $a );
/* 这里是输出:
array (size=3)
0 => int 0
1 => int 0
2 => int 2
这是我对正在发生的事情的解释:
1. The array index gets calculated which should be 0 ($a[0]), but ACTUALLY it is 1 ($a[1])
2. The rval gets calculated , which is $i++ , so the value now is 0.
3. The expression value gets calculated , which should be 1 after $i++ in the step above, but ACTUALLY it is 0.
所以从根本上说,我无法理解上面第二个示例中的步骤 1 和 3。*/