Unexpected Behavior, XOR Logical Operator - PHP

3

Why can you see this behavior of the logical operator xor ?

$bool = false xor true;
var_dump($bool); // bool(false) 

$bool = true xor false;
var_dump($bool); // bool(true)

So I read, xor should return true only if one or the other is true but not both (uniqueness), so should not return all boolean(true) ?

    
asked by anonymous 08.09.2015 / 18:20

1 answer

4

The first case is returning false because the = operator takes precedence over the xor operator.

$bool = false xor true; // false

Instead, do this:

$bool = (false xor true); // true

Source: PHP: Operator Precedence

    
08.09.2015 / 18:27