From:             geometer at ua dot fm
Operating system: *
PHP version:      Irrelevant
PHP Bug Type:     Math related
Bug description:  something about operator precedence

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

-- 
Edit bug report at http://bugs.php.net/?id=31285&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=31285&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=31285&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=31285&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=31285&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=31285&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=31285&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=31285&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=31285&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=31285&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=31285&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=31285&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=31285&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=31285&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=31285&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=31285&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=31285&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=31285&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=31285&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=31285&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=31285&r=mysqlcfg

Reply via email to