Re: PHP 4.0 Bug #6436 Updated: foreach doesn't work right in recursive functions

From: Date: Wed, 30 Aug 2000 13:46:18 +0000
Subject: Re: PHP 4.0 Bug #6436 Updated: foreach doesn't work right in recursive functions
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-31251@lists.php.net to get a copy of this message
Hello Bug, Wednesday, August 30, 2000, 3:56:57 PM, you wrote: BD> ID: 6436 BD> Updated by: jmoore BD> Reported By: kosy_denis@yahoo.com BD> Status: Closed BD> Bug Type: Scripting Engine problem BD> Assigned To: BD> Comments: BD> <?php BD> $ar = array(1,2,3,4,5,6,7,8); BD> $q = count($ar); BD> function test() BD> { BD> global $ar, $q; BD> echo "<br>in"; BD> static $max = 0; BD> $exit_flag = FALSE; BD> if ( $max < 4 ) BD> { BD> ++$max; BD> } BD> else BD> { BD> $exit_flag = TRUE; BD> } BD> if ($exit_flag == true) BD> { BD> return; BD> } BD> $i = 0; BD> foreach($ar as $a) BD> //for( $i = 0; $i < $q; ++$i ) BD> { BD> ++$i; BD> echo "<br> $max - $exit_flag - $i"; BD> if ( $i == ( $q / 2 ) ) BD> { BD> test(); BD> } BD> } BD> echo "<br>---------------"; BD> } BD> test(); ?>> BD> Works.. BD> Full Bug description available at: BD> http://bugs.php.net/?id=6436 <?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(); ?> but this piece of code outputs some other results: in 1 - - 0 1 - - 1 1 - - 2 1 - - 3 1 - - 4 in 2 - - 0 2 - - 1 2 - - 2 2 - - 3 2 - - 4 in 3 - - 0 3 - - 1 3 - - 2 3 - - 3 3 - - 4 in 4 - - 0 4 - - 1 4 - - 2 4 - - 3 4 - - 4 in 4 - 1 - 5 4 - 1 - 6 4 - 1 - 7 --------------- 4 - 1 - 5 4 - 1 - 6 4 - 1 - 7 --------------- 4 - 1 - 5 4 - 1 - 6 4 - 1 - 7 --------------- 4 - 1 - 5 4 - 1 - 6 4 - 1 - 7 --------------- with foreach: in 1 - - 1 1 - - 2 1 - - 3 1 - - 4 in 2 - - 1 2 - - 2 2 - - 3 2 - - 4 in 3 - - 1 3 - - 2 3 - - 3 3 - - 4 in 4 - - 1 4 - - 2 4 - - 3 4 - - 4 in 4 - 1 - 5 4 - 1 - 6 4 - 1 - 7 4 - 1 - 8 --------------- --------------- --------------- --------------- so after return from recursive function 'internal foreach pointer' set to end of the array. -- Best regards, Denis mailto:kosy_denis@yahoo.com __________________________________________________ Do You Yahoo!? Talk to your friends online with Yahoo! Messenger. http://im.yahoo.com

« previous php.dev (#31251) next »