Bug #73940 [Opn->Nab]: the associativity is uncertain for complex ++/-- operation

From: Date: Sun, 15 Jan 2017 05:39:37 +0000
Subject: Bug #73940 [Opn->Nab]: the associativity is uncertain for complex ++/-- operation
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206627@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73940&edit=1 ID: 73940 Updated by: requinix@php.net Reported by: hu60 dot cn at gmail dot com Summary: the associativity is uncertain for complex ++/-- operation -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Windows & Linux PHP Version: 7.1.0 Block user comment: N Private report: N New Comment: As in other languages, the order of execution is undefined. http://php.net/manual/en/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. Previous Comments: ------------------------------------------------------------------------ [2017-01-15 05:31:41] hu60 dot cn at gmail dot com Description: ------------ $x = 8; $y = ++$x; $z = $x + --$x + $x; var_dump($x, $y, $z); // output: int(8) int(9) int(24) $x = 8; $y = ++$x; $z = ($a = $x) + --$x + $x; var_dump($x, $y, $z, $a); // output: int(8) int(9) int(25) int(9) It's so amazing! Should $x + --$x + $x be equal 24 or 25? Test script: --------------- <?php $x = 8; $y = ++$x; $z = $x + --$x + $x; var_dump($x, $y, $z); echo '<hr>'; $x = 8; $y = ++$x; $z = ($c = (($a = $x) + ($b = --$x))) + ($d = $x); var_dump($x, $y, $z, $a, $b, $c, $d); echo '<hr>'; $x = 8; $y = ++$x; $z = (($x) + (--$x)) + ($x); var_dump($x, $y, $z); echo '<hr>'; $x = 8; $y = ++$x; $z = ($a = $x) + --$x + $x; var_dump($x, $y, $z, $a); ?> Expected result: ---------------- int(8) int(9) int(25) int(8) int(9) int(25) int(9) int(8) int(17) int(8) int(8) int(9) int(25) int(8) int(9) int(25) int(9) Actual result: -------------- int(8) int(9) int(24) int(8) int(9) int(25) int(9) int(8) int(17) int(8) int(8) int(9) int(24) int(8) int(9) int(25) int(9) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73940&edit=1

« previous php.bugs (#206627) next »