php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6436 foreach doesn't work right in recursive functions
Submitted: 2000-08-30 08:44 UTC Modified: 2000-08-30 08:56 UTC
From: kosy_denis at yahoo dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.2 OS: win98 Second Edition
Private report: No CVE-ID: None
 [2000-08-30 08:44 UTC] kosy_denis at yahoo dot com
<?php
    $ar = array(1,2,3,4,5,6,7,8);
    $q = count($ar);

    function test()
    {
        echo "<br>in";
        static $max = 0;
        static $exit_flag = FALSE;
        
        if ( $max < 4 )
        {
            ++$max;
        }
        else
        {
            $exit_flag = TRUE;
        }

        if ($exit_flag)
        {
            return;
        }

        global $ar, $q;
        $i = 0;
        foreach($ar as $a)
        //for( $i = 0; $i < $q; ++$i  )
        {
            ++$i;
            echo "<br> $max - $exit_flag - $i";
            if ( $i == ( $q / 2 ) )
            {
                test();
            }
        }
        echo "<br>---------------";
    }

    test();
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-30 08:56 UTC] jmoore@php.net
<?php
    $ar = array(1,2,3,4,5,6,7,8);
    $q = count($ar);

    function test()
    {
        global $ar, $q;
		echo "<br>in";
        static $max = 0;
		$exit_flag = FALSE;
        
        if ( $max < 4 )
        {
            ++$max;
        }
        else
        {
            $exit_flag = TRUE;
        }

        if ($exit_flag == true)
        {
            return;
        }

        $i = 0;
        foreach($ar as $a)
        //for( $i = 0; $i < $q; ++$i  )
        {
            ++$i;
            echo "<br> $max - $exit_flag - $i";
            if ( $i == ( $q / 2 ) )
            {
                test();
            }
        }
        echo "<br>---------------";
    }

    test();
?>

Works..
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sat Oct 10 11:00:01 2026 UTC