Re: PHP 4.0 Bug #4999: foreach() does not act like each (), uses a copy

From: Date: Tue, 13 Jun 2000 16:16:26 +0000
Subject: Re: PHP 4.0 Bug #4999: foreach() does not act like each (), uses a copy
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-21213@lists.php.net to get a copy of this message
a) foreach() uses a copy and that works without problems b) each(), next() and so on... do NOT use a copy the difference between foreach() and each() should be documented, it is not possible to let foreach() and each() work on the same pointer using this code: <? $a=array(0,1,2,3,4,5,6); // replace foreach($a as $val) { print $val; list(,$k) = each($a); echo $val.$k.' '; } ?> produces "00 11 22 33 44 55 66" not "01 23 45 6" as one could expect inserting this "strange" (I don´t know why it works, because $a=&array (0,1,2,3,4,5,6); does not work too) $a=&$a; code snipplet replacing //replace achieves the expected behaviour regards "Gustafson, Mårten" wrote:
Maybe I´ve misunderstood things, sorry if so. If foreach() uses a copy, then it shouldn´t manipulate the array pointer right? But it does. Executing the code below results in: 0123456 the while loop doesn´t output
anything. <? $a=array(0,1,2,3,4,5,6); foreach($a as $val) { print $val; } echo '<br>'; while(list($k, ) = each($a)) { echo $k; } ?> regards --marten
-----Original Message----- From: waldschrott@kiffen.de [mailto:waldschrott@kiffen.de] Sent: Tuesday, June 13, 2000 8:53 AM To: php-dev@lists.php.net Subject: [PHP-DEV] PHP 4.0 Bug #4999: foreach() does not act like each(), uses a copy
From:             waldschrott@kiffen.de
Operating system: win2000/all
PHP version:      4.0 Latest CVS (13/06/2000)
PHP Bug Type:     Documentation problem
Bug description: foreach() does not act like each(), uses a copy It should be documented that foreach() uses a copy of the passed array, being completely independent from the original $a - for the reason that each() (and next() and so on) works directly on the arrays pointer <?php $a=array(0,1,2,3,4,5,6); foreach ($a AS $val) { print $val; list(,$val)=each($a); list(,$val)=each($a); next($a); print $val; } ?>
-- 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 (#21213) next »