Logical
In PHP, you can use logical operators to combine multiple conditions and perform different actions based on those conditions.
Here are the logical operators available in PHP:
- &&: Logical AND
- ||: Logical OR
- !: Logical NOT
The && operator will return true only if both operands are true. The || operator will return true if at least one of the operands is true. The ! operator will negate the value of the operand, turning true into false and vice versa.
You can use these operators to create complex conditional statements, such as:
if (($x > 0 && $y > 0) || !$z) {
// do something
} else {
// do something else
}
This statement will execute the first block of code if either $x and $y are both greater than 0, or if $z is false. If none of these conditions are met, it will execute the second block of code.
Example:
<?php
$x = 10;
$y = 20;
$z = false;
$result1 = $x > 0 && $y > 0; // true
$result2 = $x > 0 || $z; // true
$result3 = !$z; // true
echo $result1 . "\n";
echo $result2 . "\n";
echo $result3 . "\n";
?>