Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80648 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 23497 invoked from network); 16 Jan 2015 16:22:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2015 16:22:41 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.42 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.42 mail-wg0-f42.google.com Received: from [74.125.82.42] ([74.125.82.42:41874] helo=mail-wg0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 34/00-23044-ECA39B45 for ; Fri, 16 Jan 2015 11:22:39 -0500 Received: by mail-wg0-f42.google.com with SMTP id k14so21428624wgh.1 for ; Fri, 16 Jan 2015 08:22:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=vZ/lnzfSanLkwkMispX5j0ka6ZygZ8azYnPVTyCzTc0=; b=GG2do3gWkAe7f+HjNq9YJkm6lNHApOE7Mh40sA3ateeXHBjj/MhJXKMI9t9LL2I3+v 7+/+oleNUsiT8NkD8Phea/DVNvcaC7abunPQ+Mm1uKe1yNDun/4pLDBrOdSqMm94ngP7 1RsDmjisw/1acaU4olWSl1vO46oZTs59pHWzX7FbtaVTMo3hmDyVctdddL+p9Isz3sW/ 6g14bnSD6OZ6Kc8Gqu7kMwnI5nQVzH+pyMGCV0EnzTex/C6Xr4uXJTCCD+u1e9CkXNom 2NdRkeIhD9mRbalAxH+Mg1Fvy3JEQUSzxjbf+D5QPY8lGkAh7RlzTbK8bhGu7/ppjojQ vSAQ== X-Received: by 10.180.210.236 with SMTP id mx12mr7976479wic.16.1421425355108; Fri, 16 Jan 2015 08:22:35 -0800 (PST) Received: from [192.168.0.172] ([62.189.198.114]) by mx.google.com with ESMTPSA id fg9sm3608987wib.9.2015.01.16.08.22.33 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Jan 2015 08:22:34 -0800 (PST) Message-ID: <54B93AAD.6080308@gmail.com> Date: Fri, 16 Jan 2015 16:22:05 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Fixing strange foreach behavior. From: rowan.collins@gmail.com (Rowan Collins) Yasuo Ohgaki wrote on 16/01/2015 08:40: > Hi all, > > Take a look at > > http://3v4l.org/HbVnd > > foreach should not affect internal(zval) array position, but it does. > I'm not sure why foreach works this way, but HHVM behavior is > reasonable. IMHO. > > PHP 7 would be perfect opportunity fix this behavior. > Any comments? I seem to remember somebody mentioning that the pointer is reused in some situations and not others. I think the reason is efficiency - we have a pointer associated with each array, which is rarely used, so why not borrow it for foreach? (Answer: because it causes the oddity you just spotted. :P) For instance, if you put a reset() in the middle of the foreach loop, it doesn't interrupt the progress of the loop, but does cause it to be tracked separately and output "Apple" after the loop finishes: http://3v4l.org/KTCrI There are other weird cases, like nesting two foreach loops over the same variable: - with a true array, the loops are independent (both loops see the full set of values): http://3v4l.org/lJTQG - with a rewindable Traversable, such as SimpleXMLElement, they interact (the inner loop sees all values, but the outer loop doesn't continue after the inner one completes): http://3v4l.org/fs2Jv - with a non-rewindable Traversable, such as a Generator, PHP tries to rewind and gives a fatal error, while HHVM simply uses up the last values of the generator on the inner loop (possibly a bug?): http://3v4l.org/BYOmQ My conclusion: don't try and nest foreach or any related constructs using the same variable, because the behaviour is not guaranteed to be in any way sane. Regards, -- Rowan Collins [IMSoP]