|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-12-24 13:26 UTC] derick@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 16:00:01 2025 UTC |
Description: ------------ hello it's not a bug, but an UNREASONABLE peculiarity of PHP (all versions). the peculiarity may be a reason of "strange" behavior of a script and errors may take a lot of time for their fixing. i say about precedence of some operators. let's take a look on precedence of bitwise operator "&" and comparison one "==". let me need to find out is number of the lowest significant bits in $x is less then 8 or not. as it can be done in other programming languages, i use the following construction: if ($x&0x7f==$x) {ok} else {not ok} if you try to use such a construction in a script you would be amazed, because $x&0x7f==$x IS TRUE IF AND ONLY IF $x IS EQUAL TO 0x7f! if you try to use 0x7f&$x==$x instead of original condition, you would be amazed twice, because 0x7f&$x==$x IS ALWAYS TRUE. and what the reason of this tricks? the reason is the precedence of "&" is LOWER than one of "==". so, i HAVE to write ($x&0x7f)==$x or (0x7f&$x)==$x, because taking into account the precedence one obtains: $x&0x7f==$x <=> $x&(0x7f==$x) 0x7f&$x==$x <=> 0x7f&($x==$x) such behavior is not common in programming and in mathematics. and even if i aware about the precedence, i can make a mistake "on the fly" in expression (do you like to print additional parenthesis? ;)). so, what's the reason of such particuliarly in PHP? may be it needs to be removed from the following releases? Reproduce code: --------------- <? $x = 0x76; echo $x&0x7f==$x; echo "\n"; echo 0x7f&$x==$x; ?> Expected result: ---------------- 1 1 Actual result: -------------- 0 1