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

From: Date: Tue, 25 Mar 2003 23:01:49 +0000
Subject: #22879 [Opn->Bgs]: recursive function corrupt foreach loop
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-36422@lists.php.net to get a copy of this message
ID: 22879 Updated by: helly@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: Your example is far to complex so i made it bogus. Try stripping it down and reopen the bug if you really think it is a PHP bug and not a bug in your script. The only thing that came to my mind beeing able to cause an error here is the fact you are using foreach in a nested way. So i created a really short test for that and it works with both PHP 4.3.2 and PHP 5-dev. [marcus@zaphod php4-HEAD]$ ../PHP_4_3_0/sapi/cli/php -r '$ar=array(0,1,2);foreach($ar as $n) foreach($ar as $m) echo "$n/$m\n";' 0/0 0/1 0/2 1/0 1/1 1/2 2/0 2/1 2/2 Previous Comments: ------------------------------------------------------------------------ [2003-03-25 10:14:19] lev at centers dot ru <? // Bag: recursive function corrupt foreach loop // This bag is near bag #14607 but not equal // // ------- description of functions ---------------- // Incorrect function function out_node1($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_node1($i,$level+1); } } // Correct function (bypass bag) 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) $node[]=$i; } for($i=0;$i<count($node);$i++) out_node($node[$i],$level+1); } //------------------------------------------------------------ ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> </head> <body> <? // ------- tree structure ---------------- // Tree: number parrent index // node1 0 -1 // leaf1 1 0 // leaf2 2 0 // node2 3 0 // leaf3 4 3 // leaf4 5 3 // ------- body of script ---------------- $name=Array('node1','leaf1','leaf2','node2','leaf3','leaf4'); $ind=Array('-1','0','0','0','3','3'); //------------------------------------------------------------ echo "<BR><B>Incorrect example</B><BR>\n"; // print tree out_node1(0,0); echo "<BR><B>Correct example</B><BR>\n"; // print tree out_node(0,0); ?> </body> </html> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=22879&edit=1

« previous php.bugs (#36422) next »