PHP xor Keyword
Example
Output a message only if just one of the expressions is true:
<?php
if(5 < 3 xor 5 < 10) {
echo "Only one of the expressions
was true";
}
?>
Try it Yourself »
Definition and Usage
The xor
keyword is a logical operator.
Logical operators are used to combine conditional statements.
The return value will only be true
if one of the statements is true
and the other one is
false
.
Note: This operator has lower precedence than the assignment operator, which may lead to confusing results. Wrap the expression in parentheses to avoid unexpected results.
Related Pages
Read more about operators in our PHP Operators Tutorial.
More Examples
Example
Show the difference in precedence:
<?php
$result1 = false || true;
echo "false || true = ";
echo
$result1 ? "true" : "false";
echo "<br>";
$result2 = false or
true;
echo "false or true = ";
echo $result2 ? "true" : "false";
?>
Try it Yourself »
❮ PHP Keywords