Bug #61188 [Com]: Assignment changes order of evaluation of binop expression
| From: | mineshpatel0111 at gmail dot com | Date: | Tue, 02 Aug 2016 12:58:25 +0000 |
| Subject: | Bug #61188 [Com]: Assignment changes order of evaluation of binop expression | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-202821@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
Comment by: mineshpatel0111 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:
what is wrong here ? you have assigned $a=4 that's why it is true.
$a = 3;
var_dump( $a == ( $a == 4 ) ); this will return false
Previous Comments:
------------------------------------------------------------------------
[2012-09-01 19:01:48] avp200681 at gmail dot com
Well, I understand why it works that way in PHP (and in C).
But
"Can you point to any place on the documentation where it's stated that the first
operand is evaluated first?"
I've found in PHP documentation:
"Operators on the same line have equal precedence, in which case associativity decides the
order of evaluation."
So, I think it would be good to have a note about this behaviour in PHP manual. Just to make things
clear. Because many people think that "associativity decides the order of evaluation" also
in the examples like antickon posted.
------------------------------------------------------------------------
[2012-02-26 19:43:41] antickon at gmail dot com
I assumed subexpression evaluation should be strictly left-to-right. But you are
right, it is actually not guaranteed either way. Thanks for pointing that out. I
believe it should be guaranteed strictly left-to-right, but that's just my
opinion. Proper programming practice (PrPrPr (TM)) would be not to compare
expressions with side-effects anyway.
PS. I put that comment under the page on expressions rather than the one on
operator precedence
http://www.php.net/manual/en/language.expressions.php
------------------------------------------------------------------------
[2012-02-26 19:22:56] cataphract@php.net
I think the point here is that we make no guarantee about the order of
evaluation (or apparent order, as the implementation may just be checking
whether the same variable is both sides; I haven't checked), just as C doesn't.
Can you point to any place on the documentation where it's stated that the first
operand is evaluated first?
Not that this is particularly relevant, but the only language I tried where the
behavior is what you expect is Mathematica (a = 3; (a = 4) == a is True and a =
3; a == (a = 4) is False), but it documents this behavior in
http://reference.wolfram.com/mathematica/tutorial/TheStandardEvaluationProcedure
.html
------------------------------------------------------------------------
[2012-02-26 19:18:48] antickon at gmail dot com
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.
------------------------------------------------------------------------
[2012-02-26 19:04:44] rasmus@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.
------------------------------------------------------------------------
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