Bug #63861 [Nab]: weak logic operator are buggy
| From: | rstoll at tutteli dot ch | Date: | Thu, 27 Dec 2012 12:35:34 +0000 |
| Subject: | Bug #63861 [Nab]: weak logic operator are buggy | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-175621@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63861&edit=1
ID: 63861
User updated by: rstoll at tutteli dot ch
Reported by: rstoll at tutteli dot ch
Summary: weak logic operator are buggy
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
-PHP Version: 5.4.10
+PHP Version: 5.4.7
Block user comment: N
Private report: N
New Comment:
Ah... I see, makes perfect sense this way and thus is not a bug :)
Previous Comments:
------------------------------------------------------------------------
[2012-12-27 12:31:32] nikic@php.net
The OR, XOR, AND operators are special variants of the usual logic operator with different
precedence. In particular they have lower precedence than '='.
That's why all your assignments are interpreted like this:
($a = false) or true;
Instead you should be using the normal && and || operators, those will be correctly
interpreted as
$a = (false || true);
The reason we have the lower-precedence variants is to allow things like
$result = mysql_query(...) or die(mysql_error());
This is obviously a very bad example, but probably also the one most used. It basically allows you
to combine an assignment with an error check.
This behavior is consistent with other languages that implement both the normal || and &&
operators as well as the OR and AND operators.
------------------------------------------------------------------------
[2012-12-27 10:37:13] rstoll at tutteli dot ch
Description:
------------
see test script. Seems like the weak logic operators do not have any effect.
My PHP version is 5.4.7
Was already mentioned in https://bugs.php.net/bug.php?id=46530&edit=2
Test script:
---------------
$a = false or true;
var_dump($a); //boolean false
$a = true or false;
var_dump($a); //boolean true - that's ok
$a = true xor true;
var_dump($a); //boolean true
$a = false xor true;
var_dump($a); //boolean false
$a = true and false;
var_dump($a); //boolean true
Expected result:
----------------
//or
boolean true
boolean true - this one worked as expected
//xor
boolean false
boolean true
//and
boolean false
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63861&edit=1