|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-02-26 17:54 UTC] 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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 05:00:01 2025 UTC |
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 );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);