|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-05-25 11:22 UTC] sniper@php.net
[2005-05-25 13:03 UTC] php at com dot jkkn dot dk
[2014-10-26 13:59 UTC] php at mcq8 dot be
[2015-01-08 23:36 UTC] ajf@php.net
-Status: Open
+Status: Closed
-Package: Feature/Change Request
+Package: *General Issues
-Assigned To:
+Assigned To: ajf
[2015-01-08 23:36 UTC] ajf@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 05 22:00:02 2025 UTC |
Description: ------------ When nesting foreach's on the same variable OR using it together with array_next() gives inconsistent behavour. When you use foreach() on the same variable within another foreach() you would expect either of two different behavours. Either: A) They would share the same array pointer. B) They would each have their own pointer Documentation says method A is in use... but not always it seems. Confirmed on PHP 5.0.4 and PHP 4.3.8 that this behavour is inconsistent. Below is provided two examples which show that foreach will act in two different ways when used on a by-ref variable or a normal variable. Reproduce code: --------------- / EXAMPLE 1: $numbers = range(1, 10); function printNumbers() { global $numbers; foreach ($numbers as $number) { echo $number . ', '; } } //$numbers =& $numbers; //<---- by reference (var.name does not matter) provides alternative behavour foreach ($numbers as $number) { echo $number . ": "; printNumbers(); echo "\n"; } // EXAMPLE 2: $numbers = range(1, 10); foreach ($numbers as $skip) { echo $skip . ','; next($numbers); } echo "\n"; // alternative behavour when using by-reference $numbers2 =& $numbers; foreach ($numbers2 as $skip) { echo $skip . ','; next($numbers2); } Expected result: ---------------- The code should show the same result if iterating over a normal array as iterating over a array-by-reference. Preferable both loops should show that a foreach has its own array-pointer (method B above) as the documentation clams. Actual result: -------------- EXAMPLE 2: 1,2,3,4,5,6,7,8,9,10 1,3,5,7,9