Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46529 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 39379 invoked from network); 27 Dec 2009 16:05:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Dec 2009 16:05:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lerdorf.com from 209.85.211.202 cause and error) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 209.85.211.202 mail-yw0-f202.google.com Received: from [209.85.211.202] ([209.85.211.202:35807] helo=mail-yw0-f202.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0E/09-26502-7B5873B4 for ; Sun, 27 Dec 2009 11:05:11 -0500 Received: by ywh40 with SMTP id 40so10178714ywh.26 for ; Sun, 27 Dec 2009 08:05:09 -0800 (PST) Received: by 10.100.17.30 with SMTP id 30mr21268254anq.156.1261929903871; Sun, 27 Dec 2009 08:05:03 -0800 (PST) Received: from ?192.168.200.22? (c-98-234-184-167.hsd1.ca.comcast.net [98.234.184.167]) by mx.google.com with ESMTPS id 5sm4429781yxd.71.2009.12.27.08.05.01 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 27 Dec 2009 08:05:02 -0800 (PST) Message-ID: <4B3785AC.2000507@lerdorf.com> Date: Sun, 27 Dec 2009 08:05:00 -0800 User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Ferenc Kovacs CC: Mike Wacker , internals@lists.php.net References: <31.35.26502.C79573B4@pb1.pair.com> <1B.86.26502.42B673B4@pb1.pair.com> In-Reply-To: X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Unsetting loop variables at the end of the loop From: rasmus@lerdorf.com (Rasmus Lerdorf) We can't just randomly reset variables based on their scope in this one specific case. If we are going to "fix" this, it should be done by introducing a way to do proper local scope variables. Resetting a reference simply because it is convenient in this one case would be completely inconsistent. -Rasmus Ferenc Kovacs wrote: > 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. $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? I 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 >>>> 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=php-internals&m=125617546815934&w=2. 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 search >> queries for the bug database. It 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 and >> 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. If >> you really need to hold on to a reference after the loop, then why not make >> an explicit reference? >> >> foreach ($ii as &$i) { >> $j =$ $i; >> // 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. Since re-using a loop variable is a much more common >> use case, I feel that PHP should accommodate that use case and force people >> 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 catch >> the most common use case. You could add this clause: ", especially if >> $value is used elsewhere as a loop variable again." (Though should I maybe >> move this part into the doc newsgroup?) >> >> >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> >