ID: 43501
Updated by: [EMAIL PROTECTED]
Reported By: gerry dot spm at gmail dot com
-Status: Assigned
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Linux
PHP Version: 5.2.3, 5.3CVS
Assigned To: dmitry
New Comment:
This is not a bug but expected behavior.
After exit form "foreach" loop the $elmnt variable is still refer to
the last element of array. To break the reference just do unset($elmnt)
after "foreach" loop.
Previous Comments:
------------------------------------------------------------------------
[2007-12-23 12:20:55] [EMAIL PROTECTED]
Dmitry, can you check this out please?
------------------------------------------------------------------------
[2007-12-07 02:20:26] crrodriguez at suse dot de
Right, looks like it is wrong..
Dmitry, can you please check this out ? ;-)
--TEST--
Bug #43501 foreach by reference corrupts the array
--FILE--
<?php
$foo = array(0);
foreach($foo as &$elmnt){}
var_dump($foo);
?>
--EXPECT--
array(1) {
[0]=>
int(0)
}
------------------------------------------------------------------------
[2007-12-05 08:37:44] gerry dot spm at gmail dot com
Description:
------------
bug #29992 did not prove that the array was being modified by foreach,
however this code does. According to var_dump() the last element starts
as a "string" and after the foreach is a "reference to a string".
Reproduce code:
---------------
$foo = array('three', 'blind', 'mice');
var_dump($foo);
foreach($foo as &$elmnt){}
var_dump($foo);
Expected result:
----------------
array(3) {
[0]=>
string(5) "three"
[1]=>
string(5) "blind"
[2]=>
string(4) "mice"
}
array(3) {
[0]=>
string(5) "three"
[1]=>
string(5) "blind"
[2]=>
string(4) "mice"
}
Actual result:
--------------
array(3) {
[0]=>
string(5) "three"
[1]=>
string(5) "blind"
[2]=>
string(4) "mice"
}
array(3) {
[0]=>
string(5) "three"
[1]=>
string(5) "blind"
[2]=>
&string(4) "mice"
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=43501&edit=1