#22879 [Opn->Bgs]: recursive function corrupt foreach loop
| From: | sniper@php.net | Date: | Thu, 24 Apr 2003 09:27:41 +0000 |
| Subject: | #22879 [Opn->Bgs]: recursive function corrupt foreach loop | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-38286@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 not a bag, and it's not a bug either.
Previous Comments:
------------------------------------------------------------------------
[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 " ";
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
------------------------------------------------------------------------
[2003-03-25 17:01:48] helly@php.net
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
------------------------------------------------------------------------
[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 " ";
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 " ";
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