php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login go to bug id or search bugs for
Bug #31285 Submitted: something about operator precedence 2004-12-24 13:23 UTC 2004-12-24 13:26 UTC geometer at ua dot fm Not a bug Math related Irrelevant * No None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !

[2004-12-24 13:23 UTC] geometer at ua dot fm
```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

```

## Patches

```I can't explain why we have this, but this bug report is not meant for those discussions (try the internals mailinglist, also I doubt it'll get you very far). Nevertherless, there is no change that we'll ever change this because of backward compatibility reasons.