php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31285 something about operator precedence
Submitted: 2004-12-24 13:23 UTC Modified: 2004-12-24 13:26 UTC
From: geometer at ua dot fm Assigned:
Status: Not a bug Package: Math related
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: geometer at ua dot fm
New email:
PHP Version: OS:

 

 [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

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-24 13:26 UTC] derick@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC