Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92051 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77719 invoked from network); 31 Mar 2016 17:25:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Mar 2016 17:25:33 -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.44 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.44 mail-wm0-f44.google.com Received: from [74.125.82.44] ([74.125.82.44:33528] helo=mail-wm0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2A/1D-13629-C8D5DF65 for ; Thu, 31 Mar 2016 12:25:32 -0500 Received: by mail-wm0-f44.google.com with SMTP id f198so10524322wme.0 for ; Thu, 31 Mar 2016 10:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=KOu3GUcnDG/1qWHWO6Gskku8BlwGIXHZdt63O7ve9aE=; b=PcfONhcU97ZuUds+BOv+LAfBoWCeYzIV14606/+S7WfKKw0fWa7YDJv28rufYQFQ3G w/jcq+H003g83bJ7SrOp7Ec8pNs/aGikRKvgcHc3cUg936eNQHXzKdQm2BEuxAqKl0qT 2YPCd2FtWXYaNbBUOJO898ybu4SIX8HVjQ9yI1Q+8lFF55f6XBNVZRIXKTMeQnMCDsMZ B/QyqWPSA4Z0I4dx4k0rHJF026whJVD1NJsc9YXv45AQEr6NtknSZyG3Hk2Dk4Qkmap2 Yie3tgjn/rhv06pS3RUCvsVLY34IRb8R+tl6qVeAUeDd1+39fpTn2h7lG+QkUdYAjQ+j xXKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=KOu3GUcnDG/1qWHWO6Gskku8BlwGIXHZdt63O7ve9aE=; b=EDI1YGSrd30Z1c/MSUqtPVvgsEHnm6qiS6/kXZB1SRelqNzXr6MMi92Mo2vA7y1Hqm 7sUBGvXaLgNfpi6JzH7/pgyzhKATgfwDfIO0sjFHbWwjgGqoz2DTzneHQrgOsSDp838+ dQ1fBoQOfkt9r0qt+mFjjv3ezlnskvieKEFPBgMcp52vQv9CjVlErYmXAQ7DmWwoWMv7 N28H+XgsxIMT2mobGiWFNsblVFQnTLsWJdEj0L9rzxkHIrSJIDbj/SksD9MX4uXYaMXD v3rTtxIbuhqYvm17SmU7SXvhTCY1s3W8bpJzAD3vFryDhfrG+yYsSySOnze9+wx6S56w ELgw== X-Gm-Message-State: AD7BkJJcNxTPEpx0Vsj3928woLnxjVDLV6cgmrhsKRFl3poAWYBR9ntApKaDn4/bPjKM1Q== X-Received: by 10.28.0.83 with SMTP id 80mr7253wma.76.1459445129140; Thu, 31 Mar 2016 10:25:29 -0700 (PDT) Received: from [192.168.0.77] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id u3sm25815838wmg.15.2016.03.31.10.25.28 for (version=TLSv1/SSLv3 cipher=OTHER); Thu, 31 Mar 2016 10:25:28 -0700 (PDT) To: internals@lists.php.net References: Message-ID: <56FD5D14.7020606@gmail.com> Date: Thu, 31 Mar 2016 18:23:32 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Copy On Write and Assign By Reference perform different on PHP5 and PHP7 From: rowan.collins@gmail.com (Rowan Collins) Huqiu Liao wrote on 31/03/2016 17:47: > Hi, internals, > > I have a question about Assign By Reference and I posted on StackOverflow, > I'd like to know the reason behind it, and I did not get any this kind of > answer, can anyone give me some clues. > > --- > > We have a piece of simple code: > > 1 2 $i = 2; > 3 $j = &$i; > 4 echo (++$i) + (++$i); > On PHP5, it outputs 8, because: > > $i is a reference, when we increase $i by ++i, it will change the zval > rather than make a copy, so line 4 will be 4 + 4 = 8. This is Assign By > Reference. To me, that sounds like a fixed bug. It's noteworthy in the 3v4l results that HHVM also gives 7 as the answer, and I can't see any logical reason for the answer to be 8 - regardless of execution order, the result of the two calls to ++$i should never be the same. It would be interesting to see the VLD dump in PHP 5, with and without reference, because looking at the PHP 7 one, it's clear that the two increments result in two temp vars so it shouldn't make any difference what type of zval was given as input: 4 2 PRE_INC $2 !0 # !0 is compiled $i, $2 is a new value returned 3 PRE_INC $3 !0 # !0 is referenced again, but $3 is unrelated to $2 4 ADD ~4 $2, $3 # the two values are added, with no reference to !0 Maybe in PHP 5 the opcodes are the same, but $2 and $3 somehow end up as references to !0, rather than new zvals, as though the code were: $i = 2; $i++; $temp1 =& $i; $i++; $temp2 =& $i; echo $temp1 + $temp2; rather than, as seems logical to me, HHVM, and PHP7: $i = 2; $i++; $temp1 = $i; $i++; $temp2 = $i; echo $temp1 + $temp2; Regards, -- Rowan Collins [IMSoP]