Re: & confusion, mad bug or unexpected behaviour

From: Date: Thu, 08 Jun 2000 00:44:16 +0000
Subject: Re: & confusion, mad bug or unexpected behaviour
References: 1 2  Groups: php.dev 
Request: Send a blank email to php-dev+get-20527@lists.php.net to get a copy of this message
I think *the docs* made me missing that point, because it is misleading described that the internal array cursor will be moved by one. Shouldn´t foreach() work as described, really moving the internal pointer and not the pointer of a copy, since it is deprecated to use call-time-referencing end each() move the internal cursor of the variable (and not a copy) too (I think)... regards wrote:
1) try this, works as expected: $pos=array(3=>'a',5=>'b',6=>'c',7=>'d'); $positions=&$pos; // important line foreach($positions AS $pos1 =>$char1) { list($pos2,$char2)=each($positions); echo $pos1.$pos2."<BR>"; } 2) and this, one line modified, does not work as expected, the foreach() seems not to move the cursor, change: $positions=&$pos; // important line to $positions=$pos; if it´s a bug, it´s a bad one - tested on php4 release if not I would expect another behaviour Actually, no, this is the correct behavior since foreach operates on a copy of the variable, when you assign $pos to $positions like so: $positions = &$pos; You are passing a reference to an array, a foreach loop the copies that reference to an array and operates on that, therefore, your still operating on the $pos array. However in the second example you copy the array onto the $positions variable, and then foreach operates on a copy of that, thus not modifying the $positions array at all. In this case you should just use a while loop to yield the expected behavior. Sterling sterling@designmultimedia.com -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
o----------0-¬---------O-·---¬----o---®-----o o    O   °       .
| http://www.kiffen.de | pRoteçt y0ur bRaín |0  O °     ¤  °        ·
0°·³°²'²³-¹'³´³°^°³~³²³°'³²²¨³²^³¹³²°²³`³º³°Þ ° o © ° . ·
| psychedelic experience |  gott@kiffen.de  | O   ° o           °
o-¬--o--0-----©-·--O-----o-----0-¤----------o 0 ° · ° . ¤ ·

« previous php.dev (#20527) next »