php.net |  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rstoll at tutteli dot ch
New email:
PHP Version: OS:

 

 [2012-12-27 10:37 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-27 12:31 UTC] 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 12:31 UTC] nikic@php.net
-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-2024 The PHP Group
All rights reserved.
Last updated: Sat May 11 02:01:29 2024 UTC