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