From:             lev at centers dot ru
Operating system: FreeBSD 4.6
PHP version:      4.3.1
PHP Bug Type:     Scripting Engine problem
Bug description:  recursive function corrupt foreach loop

<?
// 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 bug report at http://bugs.php.net/?id=22879&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=22879&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=22879&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=22879&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=22879&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=22879&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=22879&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=22879&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=22879&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=22879&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=22879&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=22879&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=22879&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=22879&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=22879&r=gnused

Reply via email to