From:             gree at grees dot net
Operating system: irrelevant
PHP version:      Irrelevant
PHP Bug Type:     Scripting Engine problem
Bug description:  global array used in foreach recursion

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 bug report at http://bugs.php.net/?id=29823&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=29823&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=29823&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=29823&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=29823&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=29823&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=29823&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=29823&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=29823&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=29823&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=29823&r=notwrong
Not enough info:             http://bugs.php.net/fix.php?id=29823&r=notenoughinfo
Submitted twice:             http://bugs.php.net/fix.php?id=29823&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=29823&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=29823&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=29823&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=29823&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=29823&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=29823&r=float

Reply via email to