this code produce an unexpected output:
$array=str_split("abcde");
foreach($array as &$item)
echo $item;
echo "\n";
foreach($array as $item)
echo $item;
output:
abcde
abcdd
if use &$item for second loop everything works fine.
I don’t understand how this code would affect the content of $array. I could consider that an implicit unset($header) would delete the last row but where does the double dd comes from ?
This could help:
As you can see after the last iteration
$itemrefers to 4th element of$array(e).After that you iterate over the array and change the 4th element to the current one. So after first iteration of the second loop it will be
abcda, etc toabcdd. And in the last iteration you change 4th element to 4th, asdtodAfter the first loop you should do
unset($item); it is a common practice to unset reference variable as long as you don’t need it anymore to prevent such confusions.