PHP – sucks
So, are you programming/scripting PHP?
Last week I came across a rather interesting ”feature” of PHP.
Anyway, it’s no secret PHP is not really in to pointers, which it supports.
Let’s say you want to change a value (escape or alter an object in an array (list) or whatever), maybe return that in your function, or, depending on the function parameter, do another foreach on the array.
$array = array(1,2,3,4); foreach ($array as &$value) $value++; foreach ($array as $value) echo $value;
This wonderful and logic code will give you a rather interesting result:
You did expect it to be 2345 right? 🙂
You can even find a bug-ticket about that,
and a comment which explains why this happens and that it is not a bug.
The $value reference is not deleted/cleared after the foreach.
That means in the second foreach the last element of the list (that’s where the reference is pointing to) is overwritten by one element after another by the second foreach.
When it comes to the last, the element is set to the one before the last, thus set to just that.