note 80825 rejected from control-structures.foreach by james

From: Date: Sat, 02 Feb 2008 07:39:04 +0000
Subject: note 80825 rejected from control-structures.foreach by james
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-135726@lists.php.net to get a copy of this message
Note Submitter: johannes at selfnet dot de ---- 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?

« previous php.notes (#135726) next »