big reference bug in (at least) PHP 5.0.5
| From: | Ron Korving | Date: | Fri, 16 Sep 2005 12:29:58 +0000 |
| Subject: | big reference bug in (at least) PHP 5.0.5 | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-19048@lists.php.net to get a copy of this message | ||
My apologies for my DOM-mistake today, but right now I came across something
that totally stunned me. It's a total paradox situation that just has to be
one of the biggest bugs I've ever come across in PHP. Personally, I think
it's rather high-priority because it's an engine problem, and not an issue
with some extension.
Here's the code:
<?php
$array = array();
for ($i=0; $i < 3; $i++)
$array[] = array("foo" => $i);
foreach ($array as &$item)
$item["bar"] = $item["foo"] * 2;
print_r($array);
foreach ($array as $item)
print_r($item);
?>
The expected result would be to see whatever is in the array twice. Once in
the big print_r() on $array, and once per element of $array.
Here's the actual result (look at the last array element):
Array
(
[0] => Array
(
[foo] => 0
[bar] => 0
)
[1] => Array
(
[foo] => 1
[bar] => 2
)
[2] => Array
(
[foo] => 2
[bar] => 4
)
)
Array
(
[foo] => 0
[bar] => 0
)
Array
(
[foo] => 1
[bar] => 2
)
Array
(
[foo] => 1
[bar] => 2
)
So referencing wih foreach() is obviously buggy.
With this foreach it works just fine:
foreach ($array as $i => $item)
$array[$i]["bar"] = $item["foo"] * 2;
I fear this bug is not just present in PHP 5.0.5, but in older versions too.
Ron Korving