|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-01-18 18:56 UTC] cataphract@php.net
-Status: Open
+Status: Bogus
[2011-01-18 18:56 UTC] cataphract@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sun May 31 09:00:01 2026 UTC |
Description: ------------ When using foreach to iterate over an array using the reference (&$value) syntax, and using foreach a second time using the same variable name changes the original array by overwriting the array's last item with the second-last. This is obviously wrong... Hint: note the missing object's id #4 in the actual result Test script: --------------- // the example uses objects to show the object's id in the output, but this error also occurs works with scalars class A {} $c = array(new A(), new A(), new A(), new A()); $arr = $c; // to keep the reference var_dump($arr); foreach ($arr as &$value) {} var_dump($arr); // using the same variable name foreach ($arr as $value) {} var_dump($arr); Expected result: ---------------- array(4) { [0]=> object(A)#1 (0) { } [1]=> object(A)#2 (0) { } [2]=> object(A)#3 (0) { } [3]=> object(A)#4 (0) { } } array(4) { [0]=> object(A)#1 (0) { } [1]=> object(A)#2 (0) { } [2]=> object(A)#3 (0) { } [3]=> object(A)#4 (0) { } } array(4) { [0]=> object(A)#1 (0) { } [1]=> object(A)#2 (0) { } [2]=> object(A)#3 (0) { } [3]=> object(A)#4 (0) { } } Actual result: -------------- array(4) { [0]=> object(A)#1 (0) { } [1]=> object(A)#2 (0) { } [2]=> object(A)#3 (0) { } [3]=> object(A)#4 (0) { } } array(4) { [0]=> object(A)#1 (0) { } [1]=> object(A)#2 (0) { } [2]=> object(A)#3 (0) { } [3]=> &object(A)#4 (0) { } } array(4) { [0]=> object(A)#1 (0) { } [1]=> object(A)#2 (0) { } [2]=> object(A)#3 (0) { } [3]=> &object(A)#3 (0) { } }