6

Using php 7.1.0 I'm running this little test:

<?php

$a = true;
$b = true;

$value = $a xor $b;
if ($value == true) {
    print "bad!\n";
} else {
    print "good\n";
}    

and it's coming back and saying bad. Why? An xor of two true values should be FALSE, not true.

Gargoyle
  • 9,590
  • 16
  • 80
  • 145

1 Answers1

10

The problem is operator precedence. The xor operator has lower precedence than =, so your statement is equivalent to:

($value = $a) xor $b;

You need to write:

$value = ($a xor $b);

or

$value = $a ^ $b;

The ^ operator is bit-wise XOR, not boolean. But true and false will be converted to 1 and 0, and the bit-wise results will be equivalent to the boolean results. But this won't work if the original values of the variables could be numbers -- all non-zero numbers are truthy, but when you perform bit-wise XOR with them you'll get a truthy result for any two numbers that are different.

See the PHP Operator Precedence Table

See the related Assignment in PHP with bool expression: strange behaviour

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    @Gargoyle It was presumably done for consistency with `and` and `or`. The difference is that there are also `&&` and `||` variants of them, which have higher precedence, but there's no high-precedence variant of `xor`. – Barmar Jan 23 '17 at 05:53
  • 1
    @PaulCrovella `^` is binary XOR, not boolean. – Barmar Jan 23 '17 at 05:54
  • And XOR can't do short-circuiting. That's the other difference between the boolean and binary operators, but irrelevant for XOR. – Barmar Jan 23 '17 at 06:00
  • If it is guaranteed that `$a` and `$b` are both Boolean values, `$value = $a != $b` would also work (or even `$value = $a !== $b`). – Flinsch Dec 01 '20 at 13:39