ID: 37410 Comment by: guillaume at cybercat dot ca Reported By: php at kormoc dot com Status: Verified Bug Type: Documentation problem Operating System: Linux PHP Version: Irrelevant New Comment:
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 ) Previous Comments: ------------------------------------------------------------------------ [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] [EMAIL PROTECTED] 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