website/errors/logicalXor.resultUnused.md
<?php declare(strict_types = 1);
function doFoo(bool $a, bool $b): void
{
$r = $a xor $b;
}
The result of the xor operator is not being used. The xor operator in PHP has a very low precedence -- lower than the assignment operator =. This means the expression $r = $a xor $b is actually parsed as ($r = $a) xor $b, which assigns $a to $r and then discards the result of the xor operation.
This is almost certainly not the intended behaviour. The same precedence issue applies to the and and or operators.
Use parentheses to disambiguate the logic:
<?php declare(strict_types = 1);
function doFoo(bool $a, bool $b): void
{
- $r = $a xor $b;
+ $r = ($a xor $b);
}
Or use the ^ operator instead, which has higher precedence and works as expected with booleans:
<?php declare(strict_types = 1);
function doFoo(bool $a, bool $b): void
{
- $r = $a xor $b;
+ $r = $a ^ $b;
}