note 74678 deleted from language.operators.bitwise by danielc
| From: | danielc@php.net | Date: | Mon, 13 Apr 2009 16:13:08 +0000 |
| Subject: | note 74678 deleted from language.operators.bitwise by danielc | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153165@lists.php.net to get a copy of this message | ||
Note Submitter: sgarner at expio dot co dot nz
Reason: 0
----
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