php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73940 the associativity is uncertain for complex ++/-- operation
Submitted: 2017-01-15 05:31 UTC Modified: 2017-01-15 05:39 UTC
From: hu60 dot cn at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.1.0 OS: Windows & Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: hu60 dot cn at gmail dot com
New email:
PHP Version: OS:

 

 [2017-01-15 05:31 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-15 05:39 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-01-15 05:39 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 01:00:01 2026 UTC