Logical operators help you combine or change true/false conditions in your code. They let you make decisions based on multiple rules.
Logical operators in PHP
$a && $b // AND operator $a || $b // OR operator !$a // NOT operator $a and $b // alternative AND $a or $b // alternative OR $a xor $b // exclusive OR
Use && and || for logical AND and OR with higher precedence.
and and or are similar but have lower precedence, which can affect complex expressions.
$x to false because true AND false is false.$x = true && false; var_dump($x);
$y to true because true OR false is true.$y = true || false; var_dump($y);
$z to true because NOT false is true.$z = !false; var_dump($z);
$a to true because exactly one is true (exclusive OR).$a = true xor false; var_dump($a);
This program checks if a user is logged in and an admin. It prints different messages based on these conditions.
<?php $is_logged_in = true; $is_admin = false; if ($is_logged_in && $is_admin) { echo "Access granted"; } elseif ($is_logged_in && !$is_admin) { echo "Access limited"; } else { echo "Access denied"; }
Remember that && and and are not exactly the same because of operator precedence.
Use parentheses ( ) to make complex conditions clear and avoid mistakes.
Logical operators always return boolean values true or false, except xor which returns true only if exactly one condition is true.
Logical operators combine true/false values to control program flow.
Use && for AND, || for OR, and ! for NOT.
Be careful with operator precedence and use parentheses to keep conditions clear.
Practice
true only if both conditions are true?Solution
Step 1: Understand the AND operator
The AND operator&&returns true only if both conditions it connects are true.Step 2: Compare with other operators
The OR operator||returns true if at least one condition is true, NOT!negates a condition, andxorreturns true if exactly one condition is true.Final Answer:
&& (AND) -> Option AQuick Check:
AND = both true [OK]
- Confusing OR (||) with AND (&&)
- Thinking NOT (!) combines two conditions
- Assuming xor behaves like AND
Solution
Step 1: Identify the OR operator syntax
In PHP, the logical OR operator is written as||.Step 2: Differentiate from other operators
&&is AND,!is NOT, andandis a lower precedence alternative but not the common OR operator symbol.Final Answer:
|| -> Option CQuick Check:
OR = || symbol [OK]
- Using single | instead of ||
- Confusing AND (&&) with OR (||)
- Using 'and' keyword instead of ||
$a = true;
$b = false;
if ($a && !$b) {
echo 'Yes';
} else {
echo 'No';
}
Solution
Step 1: Evaluate the NOT operator on $b
$b is false, so!$bbecomes true.Step 2: Evaluate the AND condition
$a is true and!$bis true, so$a && !$bis true.Final Answer:
Yes -> Option BQuick Check:
true AND true = true [OK]
- Ignoring the NOT operator effect
- Assuming false && true is true
- Confusing output strings
$x = true;
$y = false;
if ($x || ! $y) {
echo 'True';
else {
echo 'False';
}
Solution
Step 1: Check the if statement syntax
The if condition has correct parentheses and logical operators.Step 2: Check the braces for if-else blocks
The if block opens with{but does not have a closing}before the else block starts.Final Answer:
Missing closing brace for if block -> Option AQuick Check:
Braces must match for if-else [OK]
- Assuming missing parenthesis causes error
- Thinking logical OR syntax is wrong
- Ignoring missing brace errors
$a = false;
$b = true;
$c = false;
Which expression evaluates to
true?($a || $b) && !$cSolution
Step 1: Evaluate the OR inside parentheses
$a is false, $b is true, so$a || $bis true.Step 2: Evaluate the NOT operator on $c
$c is false, so!$cis true.Step 3: Evaluate the AND between results
true AND true equals true.Final Answer:
true -> Option DQuick Check:
(false OR true) AND NOT false = true [OK]
- Ignoring operator precedence
- Misapplying NOT operator
- Assuming OR returns false if one operand is false
Ready to prove it?
Pass the test and this pattern moves unknown
