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 $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 M4fkF
Output for 7.0.0 - 7.2.0
0 1 2 ["X",0,1,2]
Output for 5.4.0 - 5.6.28
0 1 X 0 1 2 1 ["X",0,1,2,1]