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 suppose it is up to you to implement undocumented behavior as you see fit, 
however peculiar it is. I'll post a comment on the operator precedence page to 
document it though.

Fyi, I tested this in Javascript and Java and they both evaluate strictly left-
to-right.


Previous Comments:
------------------------------------------------------------------------
[2012-02-26 19:04:44] ras...@php.net

I do see your argument, but you are making assumptions about how PHP handles 
sequence points in expressions which is not documented and thus not stricly 
defined.

------------------------------------------------------------------------
[2012-02-26 19:00:09] antickon at gmail dot com

What C or perl does is not the issue. The PHP documentation on operator 
precedence states parentheses force precedence, not evaluation order of 
subexpressions. You are saying that arbitrarily changing evaluation order 
depending on the type of subexpression is correct behavior. You have not yet 
provided any rational justification for this behavior, except that C and perl 
also behave this way.

------------------------------------------------------------------------
[2012-02-26 18:38:39] ras...@php.net

Then C/C++/Perl and the other C-like languages are all wrong as well then.

Try this in C:

#include <stdio.h>
int main(char *argv[], int argc) {
        int a=3;
        printf("%d\n",(a==(a=4)));
}

Or this in Perl:

$a=3;
print $a==($a=4);

------------------------------------------------------------------------
[2012-02-26 18:35:21] antickon at gmail dot com

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 );

------------------------------------------------------------------------
[2012-02-26 18:16:37] ras...@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.

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


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=61188


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

Reply via email to