#37410 [Ver->Csd]: foreach by reference exists outside of scope

From: Date: Fri, 17 Aug 2007 22:03:59 +0000
Subject: #37410 [Ver->Csd]: foreach by reference exists outside of scope
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969377662@lists.php.net to get a copy of this message
ID: 37410 Updated by: vrana@php.net Reported By: php at kormoc dot com -Status: Verified +Status: Closed Bug Type: Documentation problem Operating System: Linux PHP Version: Irrelevant New Comment: This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. "Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset()." Previous Comments: ------------------------------------------------------------------------ [2006-12-01 13:39:04] guillaume at cybercat dot ca I also consider this a bug, here's a simpler test case. I consider an unset() should be executed on the "value" variable before starting the foreach. Reproduce code: --------------- <?php $array = array(1,2,3); $item = &$array[2]; foreach( $array as $item ); print_r( $array ); ?> Expected result: ---------------- Array ( [0] => 1 [1] => 2 [2] => 3 ) Actual result: -------------- Array ( [0] => 1 [1] => 2 [2] => 2 ) ------------------------------------------------------------------------ [2006-05-11 23:36:25] judas dot iscariote at gmail dot com in bug 29992 read the "gardan at gmx dot de" comment for an explanation about why this is not bug. However, the documentation should mention this behaviour ( but IMHO PHP should raise an E_STRICT in case this syntax is used for some weird reason ) ------------------------------------------------------------------------ [2006-05-11 18:39:38] sean@php.net See http://www.zend.com/zend/week/week279.php#Heading1 S ------------------------------------------------------------------------ [2006-05-11 18:26:16] php at kormoc dot com Description: ------------ Foreach by reference does not honour scope. Basically, this was already reported at the following two bug reports: http://bugs.php.net/bug.php?id=29992 http://bugs.php.net/bug.php?id=36240 And they were closed with bogus, saying to look at the manual. Well, the manual doesn't talk about this issue at all, infact it seems to say that it should work as expected. Also, that var should not exists beyond the scope of the foreach loop, and the developer that closed the bug seems to agree (he said for whatever weird reason, and honestly, php should cater to the normal reasons, not the weird ones, and anyone can get the same responce out of setting it manually in the foreach themselves, rather then allowing the engine to do something weird). In anycase, this should at the very least get fixed up in the documentation, but I still consider this a engine bug. Reproduce code: --------------- <?php $array = array(1,2,3); foreach( $array as &$item ); foreach( $array as $item ); print_r( $array ); ?> Expected result: ---------------- Array ( [0] => 1 [1] => 2 [2] => 3 ) Actual result: -------------- Array ( [0] => 1 [1] => 2 [2] => 2 ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=37410&edit=1

« previous php.doc (#969377662) next »