RE: [PHP-DEV] Re: PHP 4.0 Bug #6436 Updated: foreach doesn't work right in recursive functions

From: Date: Wed, 30 Aug 2000 14:19:05 +0000
Subject: RE: [PHP-DEV] 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-31286@lists.php.net to get a copy of this message
> <?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 > --------------- Using 4.0.2 I get the following in 1 - FALSE - 1 1 - FALSE - 2 1 - FALSE - 3 1 - FALSE - 4 in 2 - FALSE - 1 2 - FALSE - 2 2 - FALSE - 3 2 - FALSE - 4 in 3 - FALSE - 1 3 - FALSE - 2 3 - FALSE - 3 3 - FALSE - 4 in 4 - FALSE - 1 4 - FALSE - 2 4 - FALSE - 3 4 - FALSE - 4 in 4 - TRUE - 5 4 - TRUE - 6 4 - TRUE - 7 4 - TRUE - 8 --------------- --------------- --------------- --------------- This is the output I would expect.. (I had made a typo before.. change if ($exit_flag == true) to if ($exit_flag == TRUE) There was some other issue with pl2 and foreach not resetting the array (IIRC) but if you change the foreach statement with a reset() and a while.. reset ($ar); while (list(, $value) = each ($ar)) You get the same output. I think this is an issue with you script if you are not getting the output you expected and not foreach. If I am not getting the point please explain in some more depth what you are trying to do here. yours James

« previous php.dev (#31286) next »