Why does this foreach:
$abc = array('a','b');
foreach ($abc as $k => &$a) {
echo $a;
$abc[] = 'c';
if ($k > 5) die;
}
Outputs this:
abccccc
And this one:
$abc = array('a');
foreach ($abc as $k => &$a) {
echo $a;
$abc[] = 'c';
if ($k > 5) die;
}
Outputs this:
a
I expected both foreaches behaving the same way (iterating until key = 6).
Because the second foreach doesn’t event foreaches a second time, i.e. does only one iteration, therefore even if an element is added, the addition happens when the foreach is already over (it starts with the idea that it has only one element, and it hasn’t to go on looping)