3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $array = [0, 1, 2]; $ref =& $array; // Necessary to trigger the old behavior $i = 0; foreach ($array as $k => &$val) { $i++; echo $i . ':' . $k . ':' . $val . ' '; if($i < 5 && !isset($array[$i])) $array[$i] = $val+1; if($i == 2) array_unshift($array, 'X'); } echo PHP_EOL . json_encode($array);
based on JaZt8
Output for 7.0.0 - 7.2.0
1:0:0 2:1:1 3:3:2 ["X",0,1,2]
Output for 5.4.0 - 5.6.28
1:0:0 2:1:1 3:0:X 4:1:0 5:2:1 6:3:2 7:4:1 ["X",0,1,2,1]