Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:45867 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71266 invoked from network); 22 Oct 2009 02:47:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2009 02:47:46 -0000 Authentication-Results: pb1.pair.com smtp.mail=marcot@tabini.ca; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=marcot@tabini.ca; sender-id=unknown Received-SPF: error (pb1.pair.com: domain tabini.ca from 206.190.53.35 cause and error) X-PHP-List-Original-Sender: marcot@tabini.ca X-Host-Fingerprint: 206.190.53.35 smtp130.rog.mail.re2.yahoo.com FreeBSD 4.7-5.2 (or MacOS X 10.2-10.3) (2) Received: from [206.190.53.35] ([206.190.53.35:26682] helo=smtp130.rog.mail.re2.yahoo.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 59/05-44649-0D7CFDA4 for ; Wed, 21 Oct 2009 22:47:45 -0400 Received: (qmail 61360 invoked from network); 22 Oct 2009 02:47:42 -0000 Received: from unknown (HELO ?10.0.1.7?) (marcot@99.234.162.171 with plain) by smtp130.rog.mail.re2.yahoo.com with SMTP; 22 Oct 2009 02:47:42 -0000 X-YMail-OSG: kbQYw1oVM1kiMbh0frWDFkpUlrJUBOmUBwG_jkrY_1twE0w9ow0cOAFoAyro7kMGgw-- X-Yahoo-Newman-Property: ymail-3 Mime-Version: 1.0 (Apple Message framework v1076) Content-Type: multipart/alternative; boundary=Apple-Mail-48-847071050 In-Reply-To: Date: Wed, 21 Oct 2009 22:47:41 -0400 Cc: mm w <0xcafefeed@gmail.com>, internals@lists.php.net Message-ID: <1C2CA39B-318B-4A9D-982F-CC2D3765D3A7@tabini.ca> References: <84898698-728C-44ED-BF7A-E0E02C111F68@gmail.com> <4ADFB80B.9050207@hexon.cx> <304A7A3B-9E8D-4BA5-8C01-511090D5F236@gmail.com> To: Richard K Miller X-Mailer: Apple Mail (2.1076) Subject: Re: [PHP-DEV] bug when using foreach with references? From: marcot@tabini.ca (Marco Tabini) --Apple-Mail-48-847071050 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=windows-1252; format=flowed; delsp=yes On 2009-10-21, at 10:40 PM, Richard K Miller wrote: > I don't follow. Is this really the intended behavior? It seems quite =20= > unintuitive that the original array would be modified by *empty* =20 > loops. It is intended behaviour. Consider your code; at the end of this loop: >>> $items =3D array('apple', 'banana', 'carrot'); >>> print_r($items); >>> foreach ($items as &$item) { } >> $item will contain a reference to the last item of $items. Therefore, =20= when you start the next loop: >>> foreach ($items as $item) { } >> $item will receive each of the values stored in $items, in sequence. =20 But $item is a reference to the last item in $items, so whatever you =20 write into it will also be written into the last item of the array. =20 Thus, the loop will cause these values to be written there: - On the first iteration, "apple" - On the second iteration, "banana" - On the third iteration, "banana" (because you are taking the third =20 item in the array, which you just overwrote) > > When would this behavior be desired? Probably never, but that does not make it counterintuitive=97the =20 interpreter is behaving correctly based on the data it is handling and =20= the instructions you are giving to it. The correct solution, as =20 someone has already recommended, is to unset the iterator if you want =20= to reuse it. Cheers, Marco.= --Apple-Mail-48-847071050--