#32088 [Opn]: Reference fails after use of unset() on array

From: Date: Thu, 19 May 2005 18:52:28 +0000
Subject: #32088 [Opn]: Reference fails after use of unset() on array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-79228@lists.php.net to get a copy of this message
ID: 32088 Updated by: pollita@php.net Reported By: karl at posmaster dot com dot au Status: Open Bug Type: Zend Engine 2 problem Operating System: * PHP Version: 5CVS-2005-02-28 New Comment: Even more fun: Try this variant and you'll see that $fluff not only gets reset to NULL, it gets completely unset. <?php $stuff = array('one','two'); $fluff = &$stff; var_dump($stuff); var_dump($fluff); foreach ($stuff as $key => &$value) { if($key==0){ unset ($stuff[$key]); }else{ $value='This should appear below in the var_dump() because $value is passed by reference'; } } var_dump($stuff); var_dump($fluff); ?> Previous Comments: ------------------------------------------------------------------------ [2005-03-01 00:50:34] karl at posmaster dot com dot au Although I can use a workaround for this problem (and in the meantime I have), this is still a bug in php isn't it? After using unset() on the array, in the loop, the reference breaks. ------------------------------------------------------------------------ [2005-02-28 22:57:00] tony2001@php.net You are modifying array in the foreach loop. Consider using for/while instead of foreach. See this code: <?php $stuff = array('one','two'); foreach ($stuff as $key => &$value) { $value='This should appear below in the var_dump() because $value is passed by reference'; } var_dump($stuff); ?> ------------------------------------------------------------------------ [2005-02-28 22:49:56] karl at posmaster dot com dot au - &$value is a refernce used in the foreach loop. - If the unset is commented out, the reference to $stuff[1] as $value on the second iteration of the loop works. - The call to unset() on the first iteration breaks the reference Actaul Output: array(1) { [1]=> string(3) "two" } Expected Output array(1) { [1]=> string(3) "This should appear below in the var_dump() because $value is passed by reference" } <?php $stuff = array('one','two'); foreach ($stuff as $key => &$value) { if($key==0){ unset ($stuff[$key]); }else{ $value='This should appear below in the var_dump() because $value is passed by reference'; } } var_dump($stuff); ?> ------------------------------------------------------------------------ [2005-02-28 20:18:52] sniper@php.net What reference? Please give the _EXACT_ expected result. (and shorten the example script..) ------------------------------------------------------------------------ [2005-02-27 22:51:05] karl at posmaster dot com dot au No, the bug is still present. The reference that should be present is still broken. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/32088 -- Edit this bug report at http://bugs.php.net/?id=32088&edit=1

« previous php.bugs (#79228) next »