Bug #61188 [Nab]: Assignment changes order of evaluation of binop expression
| From: | antickon at gmail dot com | Date: | Sun, 26 Feb 2012 18:35:21 +0000 |
| Subject: | Bug #61188 [Nab]: Assignment changes order of evaluation of binop expression | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-167861@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=61188&edit=1
ID: 61188
User updated by: antickon at gmail dot com
Reported by: antickon at gmail dot com
Summary: Assignment changes order of evaluation of binop
expression
Status: Not a bug
Type: Bug
Package: Variables related
Operating System: linux
PHP Version: 5.3.10
Block user comment: N
Private report: N
New Comment:
I'm afraid you're mistaken. The brackets denote a change in expression binding,
not evaluation order. If what you are saying is true
<?php
function a(){echo 'a';}
function b(){echo 'b';}
a() == (b());
would output ba since the bracketed expression would be evaluated first. However
it (correctly) outputs ab.
In fact, the brackets are completely irrelevant. I added them for clarity.
Consider the equivalent example:
<?php
$a = 3;
var_dump( $a == $a = 3 );
Previous Comments:
------------------------------------------------------------------------
[2012-02-26 18:16:37] rasmus@php.net
You are outsmarting yourself here. Look at your brackets.
$a == ($a=4)
So we do the bracketed expression first: $a=4 which sets $a to 4 obviously and
returns the value 4. So what are we left with?
$a == 4
But what is $a at this point? Well, it is 4 of course, because we just set it.
So we have:
4 == 4
If you can find a language where this expression doesn't return true, regardless
of the initial value of $a, then you should file a bug against that language.
------------------------------------------------------------------------
[2012-02-26 17:54:18] antickon at gmail dot com
Description:
------------
An assignment expression can change the order of operation for == and != (and
possibly other binops)
Test script:
---------------
<?php
$a = 3;
var_dump( $a == ( $a = 4 ) );
Expected result:
----------------
bool(false)
evaluation of $a == ( $a = 4 ) should be as follows:
left side of the comparison is evaluated (evaluates to 3)
right side of the comparison is evaluated (4 is assigned to $a, evaluates to 4)
3 == 4 finally evaluates to false
Actual result:
--------------
bool(true)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61188&edit=1