#43501 [Com]: foreach by reference corrupts the array (reopened)

From: Date: Fri, 07 Dec 2007 02:20:28 +0000
Subject: #43501 [Com]: foreach by reference corrupts the array (reopened)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-120441@lists.php.net to get a copy of this message
 ID:               43501
 Comment by:       crrodriguez at suse dot de
 Reported By:      gerry dot spm at gmail dot com
 Status:           Open
 Bug Type:         Scripting Engine problem
 Operating System: Linux
 PHP Version:      5.2.3, 5.3CVS
 New Comment:

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)
}


Previous Comments:
------------------------------------------------------------------------

[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


Thread (6 messages)

« previous php.bugs (#120441) next »