Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46527 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 36293 invoked from network); 27 Dec 2009 16:01:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Dec 2009 16:01:35 -0000 Authentication-Results: pb1.pair.com header.from=tyra3l@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=tyra3l@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.227 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 209.85.220.227 mail-fx0-f227.google.com Received: from [209.85.220.227] ([209.85.220.227:36847] helo=mail-fx0-f227.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 39/58-26502-ED4873B4 for ; Sun, 27 Dec 2009 11:01:35 -0500 Received: by fxm27 with SMTP id 27so9172384fxm.23 for ; Sun, 27 Dec 2009 08:01:32 -0800 (PST) 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:cc:content-type :content-transfer-encoding; bh=w3lVjSxTxAfMNNCsH5Pajc94GgTc82egsZ4SjiMqfAs=; b=BPXys09amWIO5PutxZwG2YY7TDWje9mDocLaoyZ4CmLuIaSruM+DYbpSj0Lg2Zcol6 sdDffxZgXJDa0cvMDB7n7QdOuahTI6ZNvfN7HA+xIOVe5XRnKScp4ucSeAsl0tfny1QX W+eCgtZ1d1JaF7Y/kRz2abUtksBJBHWrEUEZI= 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 :cc:content-type:content-transfer-encoding; b=jdZypQK2nvwna34K7YG/KXk8trpWcnzak7zQRVaAGftkKl7EPFZElESa1wD/S4HdWA R9HDiiBjGSFbsYa/w5zd2r8A7BFtxwNVVIRBYdWiEspjNauS7+yjacc/BOP5xSqfSb8y sBMEu8Z+MzCDqQy3zgajQxijQsTvrGlUI0a5c= MIME-Version: 1.0 Received: by 10.239.189.76 with SMTP id s12mr286409hbh.111.1261929691787; Sun, 27 Dec 2009 08:01:31 -0800 (PST) In-Reply-To: <1B.86.26502.42B673B4@pb1.pair.com> References: <31.35.26502.C79573B4@pb1.pair.com> <1B.86.26502.42B673B4@pb1.pair.com> Date: Sun, 27 Dec 2009 17:01:31 +0100 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: tyra3l@gmail.com (Ferenc Kovacs) On Sun, Dec 27, 2009 at 3:11 PM, Mike Wacker wrote= : > Adam Harvey wrote: >> >> 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 wil= l >>> update the last element of the array. >>> >>> In short, why doesn't PHP automatically unset $i after the loop? =C2=A0= I can't >>> think of too many cases where you would want to hold on to that referen= ce >>> after you exit the loop, but I can think of a lot of scenarios where a >>> user >>> could accidentally tamper the array by using $i in a different context >>> later >>> 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. > > Ah, so it seems that "reference" was the keyword I was missing in my sear= ch > queries for the bug database. =C2=A0It looks like I may have (re)opened a= can of > worms. > >> The really, really short version is that it would break backward >> compatibility to change it now, > > I would agree that if this did change, it would have to change in PHP 6 a= nd > not 5.2/3. > I would like to see this change in PHP6. :) >> it's useful in some (admittedly limited) cases, > > The main problem I see is that, like you said, these cases are limited. = =C2=A0If > you really need to hold on to a reference after the loop, then why not ma= ke > an explicit reference? > > foreach ($ii as &$i) { > =C2=A0$j =3D$ $i; > =C2=A0// loop body > } // PHP implicitly unset()'s $i > > In both this case and the status quo, someone has to add an extra line of > code for every loop. =C2=A0Since re-using a loop variable is a much more = common > use case, I feel that PHP should accommodate that use case and force peop= le > using references outside the loop to add the line of code instead. > >> and it's not as though it's not well documented as behaving that way. >> >> Adam > > True, but it may not hurt to make the documentation more explicit to catc= h > the most common use case. =C2=A0You could add this clause: ", especially = if > $value is used elsewhere as a loop variable again." (Though should I mayb= e > move this part into the doc newsgroup?) > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >