Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46524 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20086 invoked from network); 27 Dec 2009 13:17:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Dec 2009 13:17:56 -0000 Authentication-Results: pb1.pair.com header.from=adam@adamharvey.name; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=adam@adamharvey.name; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain adamharvey.name from 209.85.221.203 cause and error) X-PHP-List-Original-Sender: adam@adamharvey.name X-Host-Fingerprint: 209.85.221.203 mail-qy0-f203.google.com Received: from [209.85.221.203] ([209.85.221.203:59467] helo=mail-qy0-f203.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DD/95-26502-38E573B4 for ; Sun, 27 Dec 2009 08:17:56 -0500 Received: by qyk41 with SMTP id 41so4628351qyk.29 for ; Sun, 27 Dec 2009 05:17:53 -0800 (PST) MIME-Version: 1.0 Sender: adam@adamharvey.name Received: by 10.229.37.130 with SMTP id x2mr6290922qcd.15.1261919873192; Sun, 27 Dec 2009 05:17:53 -0800 (PST) In-Reply-To: <31.35.26502.C79573B4@pb1.pair.com> References: <31.35.26502.C79573B4@pb1.pair.com> Date: Sun, 27 Dec 2009 21:17:33 +0800 X-Google-Sender-Auth: 473c3a2213be93ca Message-ID: To: Mike Wacker Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Unsetting loop variables at the end of the loop From: aharvey@php.net (Adam Harvey) 2009/12/27 Mike Wacker : > PHP's documentation for foreach states that if you iterate by reference > [foreach ($ii as &$i) ...], you should unset $i after the loop. =C2=A0$i = still > points to the last element of the array - updating $i or reusing it will > update the last element of the array. > > In short, why doesn't PHP automatically unset $i after the loop? =C2=A0I = can't > think of too many cases where you would want to hold on to that reference > after you exit the loop, but I can think of a lot of scenarios where a us= er > could accidentally tamper the array by using $i in a different context la= ter > on (especially since loop variable names often are reused). This is a bit of a FAQ, frankly. May I suggest reading this thread from a couple of months ago: http://marc.info/?l=3Dphp-internals&m=3D125617546815934&w=3D2. There are some more discussions both on the list and in the bug tracker if you want to have a bit more of a dig into this. The really, really short version is that it would break backward compatibility to change it now, it's useful in some (admittedly limited) cases, and it's not as though it's not well documented as behaving that way. Adam