Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:45865 Return-Path: <0xcafefeed@gmail.com> Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67201 invoked from network); 22 Oct 2009 02:19:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2009 02:19:17 -0000 Authentication-Results: pb1.pair.com smtp.mail=0xcafefeed@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=0xcafefeed@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.226 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: 0xcafefeed@gmail.com X-Host-Fingerprint: 209.85.220.226 mail-fx0-f226.google.com Received: from [209.85.220.226] ([209.85.220.226:33167] helo=mail-fx0-f226.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7F/34-44649-421CFDA4 for ; Wed, 21 Oct 2009 22:19:17 -0400 Received: by fxm26 with SMTP id 26so11469542fxm.23 for ; Wed, 21 Oct 2009 19:19:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=j6YkQKDcOy/Etayd+10BySQ8ensB4lFUPBU0kOFy8oI=; b=eBS0LfcpXbZRUafq1dQ8VZAgTwx9q2zYMFRGiWi/QLBiOB5BjsOk+KUZbtTJRBmpcU T/0UDuEtkldaqG0YWKD5WAcMuJtocA8Ov6/bqC7SvuvnDW0yzxB7/VNnJict8XIimioz sMOyoBGT6AkNsjnsCausUZ3DAgyWVjkN8T978= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=GqTaHkbpvl4wGyeBr7OVANX3i9aUMJqSJdTdVJyNaF3fGlTQDrLoRoaz6Z4LnAjFAv /ETOJiOpo+sCEcT0oS46cXaiqrE5inyNCxfADg/mF4HN7xY9jt/qAX5dnLW665WQN9gI 1gnr81W2J9u8DnJkxygMJ1F9nWjiLTEVpRRUM= MIME-Version: 1.0 Received: by 10.204.3.14 with SMTP id 14mr1729722bkl.128.1256177953391; Wed, 21 Oct 2009 19:19:13 -0700 (PDT) In-Reply-To: <304A7A3B-9E8D-4BA5-8C01-511090D5F236@gmail.com> References: <84898698-728C-44ED-BF7A-E0E02C111F68@gmail.com> <4ADFB80B.9050207@hexon.cx> <304A7A3B-9E8D-4BA5-8C01-511090D5F236@gmail.com> Date: Wed, 21 Oct 2009 19:19:13 -0700 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] bug when using foreach with references? From: 0xcafefeed@gmail.com (mm w) Richard, enumerator exhausted, repeat your sub-sequence again you will get = it e.g pointer and pointed Best Regards On Wed, Oct 21, 2009 at 7:01 PM, Richard K Miller wrote: > Hi Jill, > >>> >> $items =3D array('apple', 'banana', 'carrot'); >>> print_r($items); >>> foreach ($items as &$item) { } >> >> $item is now a reference to the last element of $items. >>> >>> print_r($items); >>> foreach ($items as $item) { } >> >> And here, the foreach loop writes to $item for each element: thus >> assiging that value to the last element of $items. > > The last element of $items is 'carrot'. Why does it print apple, banana, > banana? > > It sounds like you're saying that $item should be left with the value > 'carrot', which makes sense, but why would the original array be modified= ? > (These are empty foreach loops.) > >>> print_r($items); >>> ?> >>> >>> // Output: >>> Array >>> ( >>> =C2=A0 =C2=A0[0] =3D> apple >>> =C2=A0 =C2=A0[1] =3D> banana >>> =C2=A0 =C2=A0[2] =3D> carrot >>> ) >>> Array >>> ( >>> =C2=A0 =C2=A0[0] =3D> apple >>> =C2=A0 =C2=A0[1] =3D> banana >>> =C2=A0 =C2=A0[2] =3D> carrot >>> ) >>> Array >>> ( >>> =C2=A0 =C2=A0[0] =3D> apple >>> =C2=A0 =C2=A0[1] =3D> banana >>> =C2=A0 =C2=A0[2] =3D> banana >>> ) >>> >>> Two bananas in the last set?! Not what I expected. >> >> You can fix your bug by using 'unset($item);' after the second >> foreach-loop. >> > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >