|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-12-16 00:37 UTC] rasmus@php.net
[2015-01-01 20:25 UTC] chealer at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 19:00:02 2025 UTC |
Description: ------------ Running two foreach loops on different parts of the same array, one by reference, one not causes unexpected results. Reproduce code: --------------- $foo = array(1,2,3,4,5); foreach ($foo as $key => &$val) { $val++; } foreach ($foo as $key => $val) { echo $val; } Expected result: ---------------- Expected result is: 23456 Actual result: -------------- Actual Result is: 23455 This is duplicate of bug #47388 which was dismissed without investigation. The problem here is two-fold: 1. The scope of foreach should not extend beyond a foreach loop. 2. $val should be re-initialized as a VALUE, not a REFERENCE. It looks like the second foreach is doing something comparable to $val = $foo[0]. It should be calling unset($val) prior to assigning anything to it. This is like going into a for loop and finding out that your explicit value for the incrementor variable didn't get set because you had used it in a previous for loop. I would have commented on #47388, but comments were disabled as it was abruptly closed. When deciding whether or not to accept this as a bug, please keep in mind that the current implementation serves no programmatic purpose and that the proposed implementation is not only more intuitive/expected, but also serves a purpose.