PHP quirks: passing an array by reference
Passing an array in php to a foreach which loops over the elements by reference can have strange behaviour.
Look at the following code:
What do you think the output would be if you run this piece of code? Because the array is never changed, the expected result would be:
If that was the case, I obviously wouldn't have made this blogpost ;-) The result of the code is:
What's going on?? Well... When you loop the first time over the array, the variable $a will be referenced to the array $array. This means that with every iteration $a points to the specified element in the array. After every element passed the foreach, $a still points to the last element in the array. In this case 'g'.
Now you have to loop again over the array. When the first element 'a' is passed to the foreach. $a will be set to 'a' but there is still a reference to $a from the previous foreach, which holds the value 'g'. Because it's a reference, the value of 'g' in the array changes to 'a'. When the second element 'b' is passed to the foreach. $a will be set to 'b' but the reference to $a still exists so the value of the referenced $a changes from 'a' to 'b' And so on.
If you print the value of the array at every iteration, you will have the following output:
What to do?? 1. Don't use references in a foreach. Really. If you have to do it, you propably are doing something wrong. 2. If you do need them, delete them after they are used:
If you do need them and you can't delete it because you need the variable elsewhere. Document your code!!! This little piece of code is very hard to debug when there's something wrong. Good documented code can save you a lot of headache.