PHP Logical Operators
PHP Logical Operator are and or not
Example | Name | Result |
---|---|---|
$a and $b | And | TRUE if both $a and $b are TRUE . |
$a or $b | Or | TRUE if either $a or $b is TRUE . |
$a xor $b | Xor | TRUE if either $a or $b is TRUE , but not both. |
! $a | Not | TRUE if $a is not TRUE . |
$a && $b | And | TRUE if both $a and $b are TRUE . |
$a || $b | Or | TRUE if either $a or $b is TRUE . |
Example for PHP Logical Operators
<?php
$a = true;
$b = false;
if($a && $b) {
echo ‘Both $a and $b are true. <br />’;
}
// Print the statement if $foo OR $bar is true
if($a || $b) {
echo ‘At least one of the variables is true. <br />’;
}
// Print the statement if $foo OR $bar is true, but not both
if($a xor $b) {
echo ‘One variable is true, and one is false. <br />’;
}
// Print the statement if $bar is NOT true
if(!$b) {
echo ‘$b is false. <br />’;
}
?>