note 74678 added to language.operators.bitwise
| From: | sgarner at expio dot co dot nz | Date: | Mon, 23 Apr 2007 05:04:35 +0000 |
| Subject: | note 74678 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125786@lists.php.net to get a copy of this message | ||
The bitwise AND operator behaves oddly inside an IF statement when used with equal arguments.
<?php
echo 9 & 8;
// outputs '8' (correct)
echo 8 & 8;
// outputs '8' (correct)
if (9 & 8 == 8) echo 'true'; else echo 'false';
// outputs 'true' (correct)
if (8 & 8 == 8) echo 'true'; else echo 'false';
// outputs 'false' (wrong!!)
?>
The solution is to add parentheses:
<?php
if ((8 & 8) == 8) echo 'true'; else echo 'false';
// outputs 'true' (correct)
?>
Tested on PHP 5.2.1
----
Server IP: 202.89.58.1
Probable Submitter: 203.167.215.145
----
Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php
Edit -- https://master.php.net/note/edit/74678
Del: integrated -- https://master.php.net/note/delete/74678/integrated
Del: useless -- https://master.php.net/note/delete/74678/useless
Del: bad code -- https://master.php.net/note/delete/74678/bad+code
Del: spam -- https://master.php.net/note/delete/74678/spam
Del: non-english -- https://master.php.net/note/delete/74678/non-english
Del: in docs -- https://master.php.net/note/delete/74678/in+docs
Del: other reasons-- https://master.php.net/note/delete/74678
Reject -- https://master.php.net/note/reject/74678
Search -- https://master.php.net/manage/user-notes.php