Bug #78829 [Fbk->Nab]: 数字传引用计算问题

From: Date: Mon, 18 Nov 2019 16:58:53 +0000
Subject: Bug #78829 [Fbk->Nab]: 数字传引用计算问题
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223783@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78829&edit=1

 ID:                 78829
 Updated by:         requinix@php.net
 Reported by:        1003674174 at qq dot com
 Summary:            数字传引用计算问题
-Status:             Feedback
+Status:             Not a bug
 Type:               Bug
 Package:            Variables related
 Operating System:   windows
 PHP Version:        7.3.11
 Block user comment: N
 Private report:     N

 New Comment:

Two bugs about the incorrect behavior in PHP 5,
  https://bugs.php.net/bug.php?id=71010
  https://bugs.php.net/bug.php?id=65580
both closed NAB due to undefined behavior with sequence points.

PHP 7's behavior does make more sense, but it's still technically undefined.


Previous Comments:
------------------------------------------------------------------------
[2019-11-18 16:33:35] requinix@php.net

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,
  ++$a;
  ++$a;
  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:27:20] requinix@php.net

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:22:23] cmb@php.net

> 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 15:12:35] requinix@php.net

It's about the undefined execution order.

https://www.php.net/manual/zh/language.operators.precedence.php
> 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
  <?php
  $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 13:11:13] cmb@php.net

Isn't 5 the output one would expect?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=78829


--
Edit this bug report at https://bugs.php.net/bug.php?id=78829&edit=1


Thread (7 messages)

« previous php.bugs (#223783) next »