➤ Logical operators are used to combine conditional statements.
Operator Name Example Result
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true
For instance:
$x = 10;
$y = -10;
// Operator - and
if ( $x > 5 and $y > -15 ) {
echo "True: $x > 5"."\n";
echo "True: $y > -15"."\n\n";
}
// Operator - or
if ( $x > 100 or $x > -1 ) {
echo "False: $x > 100"."\n";
echo "True: $x > -1"."\n\n";
}
// Operator - xor
if ($x == 10 xor $y == -10 ) {
// both conditional statements are true!
} else {
echo "Both is true \n\n";
}
// Operator - && (same as - and)
if ($y <= 0 && $y == -10 and $y != 10) {
echo "True: $y <= 0\n";
echo "True: $y = -10\n";
echo "True: $y != 10\n\n";
}
// Operator - || (same as - or)
if ($x == 1 or $x == 10){
echo "True: $x = 10\n";
echo "False: $x = 1\n\n";
}
$x = 0;
// Operator - !
if (!$x) {
echo "$x is false";
}
➤ The number 0 is considered to be false and all other numbers are considered to be true