Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70194 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 52276 invoked from network); 18 Nov 2013 16:02:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Nov 2013 16:02:22 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.176 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.176 mail-ob0-f176.google.com Received: from [209.85.214.176] ([209.85.214.176:62737] helo=mail-ob0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B3/D1-41213-C0A3A825 for ; Mon, 18 Nov 2013 11:02:21 -0500 Received: by mail-ob0-f176.google.com with SMTP id wp4so7110949obc.21 for ; Mon, 18 Nov 2013 08:02:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Tp1Ihp5PA+MythTKwofU5bHZAQyfsiWvGlWo+HwZrGs=; b=Dal/iQ9JKGssTnv0g8R7XXKKXpErxQIUhnXgrnFfSQUin4fUwSfo8z3nFJCcJvsqvG Cc5LoZBzEdykTHMw4EIBXh9f1p2Wt85TBJ7dg4isgGrCxSy6CVb3VQzFqdTjHLdrgFyg bOUxdMbWW4ud1wp5rEXUC+rvXNlbkDWbIoPAJ9MYAzbFq3J1LisgdvZq+WSaVOc26AD/ mEDK6gdwnv2KrQODdcihnqEePHBQesQ4f6bJNr7X/IwlfPqOuhNIeJSUAk/AD1+mIYNm epuBDeM0Er2CYKDeM3wuCFIfIg4fW7Ql24LV8KgRRWclbff46WlJOU3GG2a6b0KO+moZ 9TxA== MIME-Version: 1.0 X-Received: by 10.60.78.227 with SMTP id e3mr20557177oex.5.1384790538565; Mon, 18 Nov 2013 08:02:18 -0800 (PST) Received: by 10.182.54.112 with HTTP; Mon, 18 Nov 2013 08:02:18 -0800 (PST) In-Reply-To: References: Date: Mon, 18 Nov 2013 17:02:18 +0100 Message-ID: To: Nicolai Scheer Cc: PHP internals Content-Type: multipart/alternative; boundary=089e0111bca423c16704eb75ac9d Subject: Re: [PHP-DEV] foreach and current() From: nikita.ppv@gmail.com (Nikita Popov) --089e0111bca423c16704eb75ac9d Content-Type: text/plain; charset=ISO-8859-1 On Mon, Nov 18, 2013 at 3:14 PM, Nicolai Scheer wrote: > Hi all! > > Has it been always the way, that it is not possible to use current() in a > foreach loop? > > Just stumbled upon (using php 5.5.5): > > $arr = array( "a", "b", "c" ); > > foreach( $arr as $entry ) > { > #var_dump( current($arr) ); > } > > var_dump( current($arr) ); > > Which yields "false", because the internal array pointer points behind the > last element of $arr. > If I uncomment the inner var_dump(), the script outputs "b" four times. > This seems odd... I know that the documentation advises not to fiddle with > the internal array pointer in a foreach loop, but current() should not > affect it in any way and therefor should be safe to use. > > Does anyone know what the reasoning behind this behavious is? > The relation between foreach and current() is somewhat complex and depends on the environment, in particular the refcount of the array, whether it is a reference and whether the iteration happens by-reference. The exact behavior and the reasons for it are outlined in http://stackoverflow.com/questions/10057671/how-foreach-actually-works/14854568#14854568 . I have a patch attached to bug #53405 (https://bugs.php.net/bug.php?id=53405) to make foreach and current() fully independent (and provide generally stronger independence and safety guarantees in that area), but it's a somewhat tricky affair and I likely won't have time to finish this for 5.6. Nikita --089e0111bca423c16704eb75ac9d--