Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89155 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78915 invoked from network); 10 Nov 2015 19:45:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Nov 2015 19:45:13 -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 74.125.82.43 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 74.125.82.43 mail-wm0-f43.google.com Received: from [74.125.82.43] ([74.125.82.43:33921] helo=mail-wm0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7C/27-21426-64942465 for ; Tue, 10 Nov 2015 14:45:10 -0500 Received: by wmvv187 with SMTP id v187so24974758wmv.1 for ; Tue, 10 Nov 2015 11:45:07 -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=vWS2HKzvv3p88+bonv9fMAbwd+Jdgy/cUC0mF5WFSFM=; b=ZSCQs0TkjYhfWcDXUrVfxD8Pg7zz81p9ZM7kl9KnMAMxGdHBbI0f4WLGhYMVsvFY9R xtfATGm43Vp9zSygVyg7f4YX8mgGfLvaHTZA+hq25UfXWxjluuf1T843KBMnvZjMm1Ww ePn7CQgfnhaOnYkPCtuc2bIxrcpzn804N5zSd9mQJdR9Luh6gRHGpQ2oeRomkq2ToH7a il1ZKw+TIBZzayJ7S5G5yWsjhgDNTQaPSg0rX/MleCxDVVvmJdXSbuC/39eLF4uiRHnP Me39pLeChUyv/5NagfXTrLmCiNKdPoHVu2+9M21sTu7Gcrk8Y727pXzo+4wNRW7+xaGS fJjw== MIME-Version: 1.0 X-Received: by 10.28.35.203 with SMTP id j194mr2340705wmj.13.1447184707172; Tue, 10 Nov 2015 11:45:07 -0800 (PST) Received: by 10.27.84.70 with HTTP; Tue, 10 Nov 2015 11:45:07 -0800 (PST) In-Reply-To: References: Date: Tue, 10 Nov 2015 20:45:07 +0100 Message-ID: To: Philip Hofstetter Cc: PHP internals Content-Type: multipart/alternative; boundary=001a113eb396654363052434f2db Subject: Re: [PHP-DEV] PHP7 / foreach / references / ugly code / discrepancy to PHP 5.6 From: nikita.ppv@gmail.com (Nikita Popov) --001a113eb396654363052434f2db Content-Type: text/plain; charset=UTF-8 On Tue, Nov 10, 2015 at 5:48 PM, Philip Hofstetter < phofstetter@sensational.ch> wrote: > Hi, > > I'm having a cause of slightly ugly code that runs differently from PHP 5.6 > to PHP 7 and I don't think this difference is documented, nor expected. As > I'm not sure whether this is an intended change or not, I'm asking here > firstr before making a bug report. > > The code in question is > > ---- > $b = [1, 2, 3]; > > foreach($b ?: [] as &$v){ > $v = $v*2; > unset($v); > } > > var_dump($b); > ---- > in PHP7, this produces > > array(3) { > [0] => > int(1) > [1] => > int(2) > [2] => > int(3) > } > > whereas in PHP 5.6 and earlier, this produces > > array(3) { > [0] => > int(2) > [1] => > int(4) > [2] => > int(6) > } > > what scares me about this is that in order to get the old behaviour in PHP7 > back, you just have to get rid of the ?: operator: > > foreach($b as &$v){ > > is fine > > foreach($b ?: [] as &$v){ > > is not. > > I guess this is related to > > http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.foreach.by-value > or > > http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.foreach.by-ref > , > but I'm not entirely sure whether this is actually intended behaviour - > it's certainly unexpected to me that applying ?: now always makes a copy > whereas before it didn't. > This is a bug in PHP 5, which has been fixed in PHP 7 as a side-effect of other changes. The new behavior is correct. This issue is tracked at https://bugs.php.net/bug.php?id=70857. Nikita --001a113eb396654363052434f2db--