|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63861 weak logic operator are buggy
Submitted: 2012-12-27 10:37 UTC Modified: 2012-12-27 12:35 UTC
From: rstoll at tutteli dot ch Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.7 OS:
Private report: No CVE-ID: None
 [2012-12-27 10:37 UTC] rstoll at tutteli dot ch
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

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:
boolean true
boolean true - this one worked as expected

boolean false
boolean true

boolean false


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-27 12:31 UTC]
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 12:31 UTC]
-Status: Open +Status: Not a bug
 [2012-12-27 12:35 UTC] rstoll at tutteli dot ch
-PHP Version: 5.4.10 +PHP Version: 5.4.7
 [2012-12-27 12:35 UTC] rstoll at tutteli dot ch
Ah... I see, makes perfect sense this way and thus is not a bug :)
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:26 2019 UTC