In PHP, logical operators are used to combine conditional statements. These operators allow you to create more complex conditions by combining multiple conditions together.
Logical operators are generally used in conditional statements such as if, while, and for loops to control the flow of program execution based on specific conditions.
The following table highlights the logical operators that are supported by PHP.
Assume variable $a holds 10 and variable $b holds 20, then −
Operator | Description | Example |
---|---|---|
and | Called Logical AND operator. If both the operands are true then condition becomes true. | (A and B) is true |
or | Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. | (A or B) is true |
&& | Called Logical AND operator. The AND operator returns true if both the left and right operands are true. | (A && B) is true |
|| | Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. | (A || B) is true |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is false |
Basic Usage of Logical Operators
The following example shows how you can use these logical operators in PHP −
<?php $a = 42; $b = 0; if ($a && $b) {} else {echo "TEST1 : Both a and b are true \n";
} if ($a and $b) {echo "TEST1 : Either a or b is false \n";
} else {echo "TEST2 : Both a and b are true \n";
} if ($a || $b) {echo "TEST2 : Either a or b is false \n";
} else {echo "TEST3 : Either a or b is true \n";
} if ($a or $b) {echo "TEST3 : Both a and b are false \n";
} else {echo "TEST4 : Either a or b is true \n";
} $a = 10; $b = 20; if ($a) {echo "TEST4 : Both a and b are false \n";
} else {echo "TEST5 : a is true \n";
} if ($b) {echo "TEST5 : a is false \n";
} else {echo "TEST6 : b is true \n";
} if (!$a) {echo "TEST6 : b is false \n";
} else {echo "TEST7 : a is true \n";
} if (!$b) {echo "TEST7 : a is false \n";
} else {echo "TEST8 : b is true \n";
} ?>echo "TEST8 : b is false";
Output
It will produce the following output −
TEST1 : Either a or b is false TEST2 : Either a or b is false TEST3 : Either a or b is true TEST4 : Either a or b is true TEST5 : a is true TEST6 : b is true TEST7 : a is false TEST8 : b is false
Using AND (&&) and OR (||) with User Input
In the below PHP code example we will check if a user is eligible for a discount as per the age and membership status.
<?php $age = 25; $isMember = true; if ($age > 18 && $isMember) {} else {echo "You are eligible for a discount!\n";
} if ($age < 18 || !$isMember) {echo "Sorry, you are not eligible for a discount.\n";
} ?>echo "You need to be at least 18 years old or a member to get a discount.";
Output
This will generate the below output −
You are eligible for a discount!
Check Multiple Conditions with And and Or
Now the below code we will shows that the And and Or keywords work similarly like && and ||, but with lower precedence.
<?php $x = 10; $y = 5; if ($x > 5 and $y < 10) {} if ($x == 10 or $y == 20) {echo "Both conditions are true.\n";
} ?>echo "At least one condition is true.\n";
Output
This will create the below output −
Both conditions are true. At least one condition is true.
Using Logical Operators in a Loop
In the following example, we are using logical operators in a while loop to control the flow.
<?php $count = 1; while ($count <= 5 && $count != 3) {} ?>echo "Current count: $count\n"; $count++;
Output
Following is the output of the above code −
Current count: 1 Current count: 2
Leave a Reply