|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78829 数字传引用计算问题
Submitted: 2019-11-18 09:35 UTC Modified: 2019-11-18 16:58 UTC
From: 1003674174 at qq dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.3.11 OS: windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: 1003674174 at qq dot com
New email:
PHP Version: OS:


 [2019-11-18 09:35 UTC] 1003674174 at qq dot com

Test script:
$a=1;$b=&$a;echo (++$a)+(++$a);exit;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-18 13:11 UTC]
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2019-11-18 13:11 UTC]
Isn't 5 the output one would expect?
 [2019-11-18 15:12 UTC]
-Status: Feedback +Status: Not a bug -Assigned To: cmb +Assigned To:
 [2019-11-18 15:12 UTC]
It's about the undefined execution order.
> Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of
> evaluation. PHP does not (in the general case) specify in which order an expression is evaluated and code that
> assumes a specific order of evaluation should be avoided, because the behavior can change between versions of PHP
> or depending on the surrounding code.
> Example #2 Undefined order of evaluation
  $a = 1;
  echo $a + $a++; // may print either 2 or 3

  $i = 1;
  $array[$i] = $i++; // may set either index 1 or 2

(the page is already marked as needing a translation update)
 [2019-11-18 16:22 UTC]
> It's about the undefined execution order.

I don't think so, since it doesn't matter whether `(++$a)` is
evaluated before `(++$a)` or the other way round.  It rather
seems to me that PHP < 7 got confused by the reference.
 [2019-11-18 16:27 UTC]
-Status: Not a bug +Status: Feedback
 [2019-11-18 16:27 UTC]
Sorry yes, you're right, it's a pre-increment in there, and the reference does (did) matter.

5 is the correct output - I'm not sure how it came up with 6 before.
 [2019-11-18 16:33 UTC]
Of course the minute after I send that I figure it out.

The reference mattered because ++$a incremented $a and then "returned" $a. In other words,
  echo ($a)+($a);

When PHP 7 changed references and introduced AST and redid everything else, that nuance disappeared and the expression would properly return the *value of* $a, regardless of whether the variable was a reference or not.
While I don't remember a specific example, I know there have been a few bug reports about this same change in behavior with similar situations. I might be able to find one...
 [2019-11-18 16:58 UTC]
-Status: Feedback +Status: Not a bug
 [2019-11-18 16:58 UTC]
Two bugs about the incorrect behavior in PHP 5,
both closed NAB due to undefined behavior with sequence points.

PHP 7's behavior does make more sense, but it's still technically undefined.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 19:01:30 2024 UTC