#22879 [Opn->Bgs]: recursive function corrupt foreach loop

From: Date: Mon, 28 Apr 2003 15:30:00 +0000
Subject: #22879 [Opn->Bgs]: recursive function corrupt foreach loop
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-38590@lists.php.net to get a copy of this message
ID: 22879 Updated by: sniper@php.net Reported By: lev at centers dot ru -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: FreeBSD 4.6 PHP Version: 4.3.1 New Comment: It's _BUG_ if anything, I have no idea what 'BAG' in this context would be. :) And it's _expected behaviour_, thus it's not a bug in PHP, or in your script. You just try to do something that you shouldn't do. Previous Comments: ------------------------------------------------------------------------ [2003-04-24 10:11:17] lev at centers dot ru Script works incorrect in case: 1 script has error 2 bag in PHP. If script has no error, then there is bag in PHP. I can’t see error in my script. So I think it is bag. Modification of script make situation where bag don’t appear. I call this “bypass bag”. As result: If script works incorrect on deferent computers and anybody can’t find error in script – it means BAG. ------------------------------------------------------------------------ [2003-04-24 04:27:40] sniper@php.net It's not a bag, and it's not a bug either. ------------------------------------------------------------------------ [2003-04-24 01:41:21] lev at centers dot ru Yes, it works fine. But it is only bypass of bag ------------------------------------------------------------------------ [2003-04-23 04:38:03] sniper@php.net Just pass the $ind array as parameter always to the out_node1() and it works fine. Not bug. ------------------------------------------------------------------------ [2003-03-26 06:01:56] lev at centers dot ru "Nested foreach" and "recursive function call into foreach loop" is deferent things. I guess: control variable of foreach loop is global and corrupted after recursive function call. Compact variant of example: -------------------------- <? // Bag: recursive function corrupt foreach loop function out_node($k,$level) { global $name,$ind; for($i=0;$i<$level;$i++) echo "&nbsp;&nbsp;&nbsp;"; echo "$name[$k]<BR>\n"; foreach($ind as $i=>$index) { if($index==$k) out_node($i,$level+1); } } ?> <html> <head> </head> <body> <? $name=Array('node1','leaf1','leaf2','node2','leaf3','leaf4'); $ind=Array('-1','0','0','0','3','3'); out_node(0,0); ?> </body> </html> Result: ------- node1 leaf1 must be (Correct result): ------------------------ node1 leaf1 leaf2 node2 leaf3 leaf4 ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/22879 -- Edit this bug report at http://bugs.php.net/?id=22879&edit=1

« previous php.bugs (#38590) next »