Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89177 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62971 invoked from network); 11 Nov 2015 13:33:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Nov 2015 13:33:57 -0000 Authentication-Results: pb1.pair.com header.from=dz@heroku.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=dz@heroku.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain heroku.com designates 74.125.82.43 as permitted sender) X-PHP-List-Original-Sender: dz@heroku.com X-Host-Fingerprint: 74.125.82.43 mail-wm0-f43.google.com Received: from [74.125.82.43] ([74.125.82.43:36855] helo=mail-wm0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 99/F1-48205-3C343465 for ; Wed, 11 Nov 2015 08:33:56 -0500 Received: by wmww144 with SMTP id w144so159516532wmw.1 for ; Wed, 11 Nov 2015 05:33:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=heroku_com.20150623.gappssmtp.com; s=20150623; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=9zxU7pnDImBRQRDbKtzjXy8Z5/8LstvLKebbOtP9D3I=; b=lOUyOYaZdN/Va5L/DrKu/+DxEKancoTgLRd2B1hSefX8jYq/Mi471nKVdOK/fuzhOq fPj7Yu2mLIm71fLw5SnBXd+uX1DoVhc0kXwZY6gHMTjUjrL3GKvpeIF7pigO5K5hc7B7 y+Z2rwx+4VN1rxitgLezh4Wz2J4rYLe6ywyc2qe6CauXSmcrGSafQRMTQclmfURUZvOs ftFmM24ZXkXMicyTtCfZfivLqQv2aQRVM+Gj2//UF/vRMzkX38AyvVIYtf6WtRPZN5ay UknKR8iJCz76ctRI3HwsRbcEYY8EOaY5YpUSLM527tTX0GgI7J69WsCFShmuVznYy0oi o42g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:content-type:mime-version:subject:from :in-reply-to:date:cc:content-transfer-encoding:message-id:references :to; bh=9zxU7pnDImBRQRDbKtzjXy8Z5/8LstvLKebbOtP9D3I=; b=grpE3augOGF3bE/bXT/0GKgqABpnNITTRJ0uwEsP8snAoV8XvUp11xkXlrxDbTxpe2 XNoZxwXogDcQ6LI8iX+6cqrs65k3izbOWYvQEe3W9WktcKDKguB+hEdmeRwhBl/Ijrw0 a5iCXSP5h57yHYKQds7Y9iEf5Yon6DAIiEzK66c8UdOjW0ILXv9LYGKivXz6U8pC2IQy apIWBrmXBD1QGFVC3SijUIHeF+/nNQGPp9LcjJL5gWwvonMCPJVjeGd5qvlN7OF9shhE P1XUAI1UwuC8Qb043rB0ILAcY+d/B+WShNU2pK3z72IxTsG5J25ldcLqZTD8beAJJNu2 /2aA== X-Gm-Message-State: ALoCoQmp0Crf0wGu0F5RmkscHZEyCfpCKedTIYNE7iYIHHIzQRrJ9IXkn08wMDSAhc1UHGG9LO7Z X-Received: by 10.194.134.199 with SMTP id pm7mr10033463wjb.46.1447248832405; Wed, 11 Nov 2015 05:33:52 -0800 (PST) Received: from [172.18.67.187] (35.43-246-81.adsl-static.isp.belgacom.be. [81.246.43.35]) by smtp.gmail.com with ESMTPSA id e63sm25344348wma.7.2015.11.11.05.33.51 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 11 Nov 2015 05:33:51 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) In-Reply-To: Date: Wed, 11 Nov 2015 14:33:49 +0100 Cc: Philip Hofstetter , PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: References: To: Nikita Popov X-Mailer: Apple Mail (2.2104) Subject: Re: [PHP-DEV] PHP7 / foreach / references / ugly code / discrepancy to PHP 5.6 From: dz@heroku.com (David Zuelke) Should this be in the UPGRADING notes? > On 10.11.2015, at 20:45, Nikita Popov wrote: >=20 > On Tue, Nov 10, 2015 at 5:48 PM, Philip Hofstetter < > phofstetter@sensational.ch> wrote: >=20 >> Hi, >>=20 >> 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. >>=20 >> The code in question is >>=20 >> ---- >> $b =3D [1, 2, 3]; >>=20 >> foreach($b ?: [] as &$v){ >> $v =3D $v*2; >> unset($v); >> } >>=20 >> var_dump($b); >> ---- >> in PHP7, this produces >>=20 >> array(3) { >> [0] =3D> >> int(1) >> [1] =3D> >> int(2) >> [2] =3D> >> int(3) >> } >>=20 >> whereas in PHP 5.6 and earlier, this produces >>=20 >> array(3) { >> [0] =3D> >> int(2) >> [1] =3D> >> int(4) >> [2] =3D> >> int(6) >> } >>=20 >> 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: >>=20 >> foreach($b as &$v){ >>=20 >> is fine >>=20 >> foreach($b ?: [] as &$v){ >>=20 >> is not. >>=20 >> I guess this is related to >>=20 >> = http://php.net/manual/en/migration70.incompatible.php#migration70.incompat= ible.foreach.by-value >> or >>=20 >> = http://php.net/manual/en/migration70.incompatible.php#migration70.incompat= ible.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. >>=20 >=20 > 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=3D70857. >=20 > Nikita