Edit report at http://bugs.php.net/bug.php?id=52418&edit=1
ID: 52418 Updated by: [email protected] Reported by: joe dot vallet at gmail dot com Summary: foreach returns inconsistent results -Status: Open +Status: Bogus Type: Bug Package: *Programming Data Structures Operating System: Linux PHP Version: 5.3.3 New Comment: Foreach has some unusual semantics when it's used with a reference due to the lack of block scope in PHP, hence the warning at http://au2.php.net/manual/en/control-structures.foreach.php advising against the reuse of a referenced iterator variable. A number of bugs have previously been opened about this behaviour, including most recently bug #51409, bug #50582 and bug #50485 (and probably many, many others going further back). This behaviour won't be changed, due to backward compatibility concerns. Previous Comments: ------------------------------------------------------------------------ [2010-07-23 15:20:46] joe dot vallet at gmail dot com Description: ------------ I make a loop with a "foreach" and each item is passed by reference. After what I make another loop on the same array with the same item var name, but not by reference this time. Then the last value of the array is replaced by next to last one. Test script: --------------- <?php $idList = array(42, 52, 101010); foreach($idList as &$id) {}; print_r($idList); // => Array ( [0] => 42 [1] => 52 [2] => 101010 ) foreach($idList as $id) {}; print_r($idList); // => Array ( [0] => 42 [1] => 52 [2] => 52 ) // == ?> Expected result: ---------------- Array ( [0] => 42 [1] => 52 [2] => 101010 ) Array ( [0] => 42 [1] => 52 [2] => 101010 ) Actual result: -------------- Array ( [0] => 42 [1] => 52 [2] => 101010 ) Array ( [0] => 42 [1] => 52 [2] => 52) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52418&edit=1
