Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
Operator | Description | Example |
---|---|---|
&& and |
And | $a and $b returns TRUE if both $a and $b are TRUE. && has a higher precedence than and. |
|| or |
Or | $a or $b returns TRUE if either $a or $b is TRUE. || has a higher precedence than or. |
! | Not | !$a returns TRUE if $a is not TRUE. |
xor | Xor | $a xor $b returns TRUE if either $a or $b is TRUE, bjut not both. |
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php // "&&" has a greater precedence than "and" $e = true && false; // $e will be assigned to (true && false) which is false $f = true and false; // $f will be assigned to true echo "The value of e = true && false is: ", $e, "<br />"; echo "The value of f = true and false is: ", $f, "<br />"; // "||" has a greater precedence than "or" $g = false || true; // $g will be assigned to (false || true) which is true $h = false or true; // $h will be assigned to false echo "The value of g = false || true is: ", $g, "<br />"; echo "The value of h = false or true is: ", $h, "<br />"; $a = 1; $b = 2; $c = 5; echo "a = ", $a,"<br />"; echo "b = ", $b,"<br />"; echo "c = ", $c,"<br />"; echo "The value of !($a > $b && $b < $c) is: ", !($a > $b && $b < $c), "<br />";// true echo "The value of (($a > $b) and ($b < $c)) is: ", (($a > $b) and ($b < $c)), "<br />";// false echo "The value of ($a == $b or $b < $c) is: ", ($a == $b or $b < $c), "<br />"; // true echo "The value of $a == $b || $b < $c is: ", $a == $b || $b < $c, "<br />"; // true $x = $a < $b; //$x = true $y = $b === $c; //$y = false echo "x = $a < $b is: ", $x,"<br />"; echo "y = $b == $c is: ", $y,"<br />"; echo "The value of x xor y is: ", $x xor $y; // true ?> </p> </body> </html> |