note 80825 added to control-structures.foreach
| From: | johannes at selfnet dot de | Date: | Sat, 02 Feb 2008 00:54:16 +0000 |
| Subject: | note 80825 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135720@lists.php.net to get a copy of this message | ||
given the following sample script i get completely different results for different versions of php.
<?php
function rec(&$data,$i)
{
foreach($data as $n)
{
echo "$n$i ";
if($i<2)
{
rec($data,$i+1);
}
}
}
$x = array('A','B');
rec($x,1)
?>
the wanted behaviour was that the output is "A1 A2 B2 B1 A2 B2". for the php versions
5.1.2 and 5.2.0 i ony get "A1 A2 B2" instead. it seems as if the loop reference is passed
with the variable $data so that after returning from the recursion the loop is left.
on the other hand php 5.2.3 and versions below 5.1.2 do handle it "correctly". the loop
reference is kept within the namespace of each instance of the funcion.
can anyone verify whether this behaviour was intented by the developers and if yes why did they
switch back then?
----
Server IP: 217.13.201.10
Probable Submitter: 85.180.56.135
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- https://master.php.net/note/edit/80825
Del: integrated -- https://master.php.net/note/delete/80825/integrated
Del: useless -- https://master.php.net/note/delete/80825/useless
Del: bad code -- https://master.php.net/note/delete/80825/bad+code
Del: spam -- https://master.php.net/note/delete/80825/spam
Del: non-english -- https://master.php.net/note/delete/80825/non-english
Del: in docs -- https://master.php.net/note/delete/80825/in+docs
Del: other reasons-- https://master.php.net/note/delete/80825
Reject -- https://master.php.net/note/reject/80825
Search -- https://master.php.net/manage/user-notes.php