#43501 [Asn->Opn]: foreach by reference corrupts the array

From: Date: Sun, 23 Dec 2007 12:20:33 +0000
Subject: #43501 [Asn->Opn]: foreach by reference corrupts the array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-120831@lists.php.net to get a copy of this message
ID: 43501 Updated by: jani@php.net Reported By: gerry dot spm at gmail dot com -Status: Assigned +Status: Open Bug Type: Scripting Engine problem Operating System: Linux PHP Version: 5.2.3, 5.3CVS Assigned To: dmitry Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#120831) next »