php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29823 global array used in foreach recursion
Submitted: 2004-08-24 20:06 UTC Modified: 2004-12-15 04:04 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: gree at grees dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gree at grees dot net
New email:
PHP Version: OS:

 

 [2004-08-24 20:06 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-15 04:04 UTC] sniper@php.net
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."


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 11 10:01:27 2024 UTC