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

 ID:                 63978
 Updated by:         cataphr...@php.net
 Reported by:        bobwei9 at hotmail dot com
 Summary:            Strange behaviour with precedence order
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Irrelevant (OS X 10.8)
 PHP Version:        master-Git-2013-01-12 (Git)
 Block user comment: N
 Private report:     N

 New Comment:

+ is documented as having left-to-right associativity. So:

+ $i + ($i += 3)

is the same as

(0 + $i) + ($i += 3)

Which of the two operands to the "outside" addition is evaluate first is not 
specified anywhere in the manual. The implementation evaluates the first 
argument first in this case. In your other example, the first operand is just a 
variable, so PHP uses that variable directly in the final addition, at which 
point it's value is already 3.

To sum up, you cannot expect the operands to be evaluated in any specific order.


Previous Comments:
------------------------------------------------------------------------
[2013-01-12 17:46:13] bobwei9 at hotmail dot com

Description:
------------
I think the test script should be self explaining.

Order is somewhat strange.

Once it seems to be evaluated as

$first = (+$i); // => 0
$second = ($i += 3); // => 3
$result = $first + $second; // => 3

and once as:

$second = ($i += 3); // => 3
$first = ($i); // => 3
$result = $first + $second; // => 6

Test script:
---------------
php > $i = 0; print (+ $i + ($i += 3))."\n";
3
php > $i = 0; print ($i + ($i += 3))."\n";
6

Expected result:
----------------
I'd expect that the parenthesis are only resolved if encountered and not first 
resolved, then the variable replaced by its value.



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



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

Reply via email to