ID: 29823 Updated by: [EMAIL PROTECTED] Reported By: gree at grees dot net -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: irrelevant PHP Version: Irrelevant New Comment:
This is same as bug #26396 is about. And this too: "Note: Also note that foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array." Previous Comments: ------------------------------------------------------------------------ [2004-08-24 20:06:35] gree at grees dot net Description: ------------ When is used globalized array into recursion method, there is an odd behaving of foreach on this array in each recursion call. It seems, that there is no copy of array for foreaching, but reference??? - internal array pointer is changed in should-be-copy of array. (ref.(php-manual: foreach): Note: Also note that foreach operates on a copy of the specified array and not the array itself.) (btw: it is not bug #22879) Reproduce code: --------------- <?php //--------- ODD ------- echo "<br>global:<br>"; $globvar = array(1,2,3); Recursion(1); function Recursion($nr) { global $globvar; echo "in:".$nr."<br>"; foreach($globvar as $valnr) { if($valnr > $nr) { echo $valnr."<br>"; Recursion($valnr); } } echo "out:".$nr."<br>"; } //-------- EXPECTED ----------- echo "<br>local:<br>"; $locvar = array(1,2,3); RecursionLoc(1, $locvar); function RecursionLoc($nr, $locvar) { echo "in:".$nr."<br>"; foreach($locvar as $valnr) { if($valnr > $nr) { echo $valnr."<br>"; RecursionLoc($valnr, $locvar); } } echo "out:".$nr."<br>"; } ?> Expected result: ---------------- global: in:1 2 in:2 3 in:3 out:3 out:2 3 in:3 out:3 out:1 local: in:1 2 in:2 3 in:3 out:3 out:2 3 in:3 out:3 out:1 Actual result: -------------- global: in:1 2 in:2 3 in:3 out:3 out:2 out:1 local: in:1 2 in:2 3 in:3 out:3 out:2 3 in:3 out:3 out:1 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=29823&edit=1