What is the difference between the logical operators OR and XOR in PHP, and how can they affect conditional statements?
The logical operator OR (||) in PHP evaluates to true if either of the operands is true. On the other hand, the XOR operator (^) evaluates to true only if one of the operands is true, but not both. When used in conditional statements, OR can result in the statement being true if either condition is met, while XOR will only be true if one condition is met exclusively.
// Example of using OR operator in a conditional statement
$a = 5;
$b = 10;
if ($a == 5 || $b == 10) {
echo "At least one condition is true.";
}
// Example of using XOR operator in a conditional statement
$c = 15;
$d = 20;
if ($c == 15 ^ $d == 20) {
echo "Only one condition is true.";
}