Logical operators in PHP - Time & Space Complexity
Logical operators combine true or false values to make decisions in code.
We want to see how the time to run code with logical operators changes as input grows.
Analyze the time complexity of the following code snippet.
function checkConditions(array $data) {
foreach ($data as $item) {
if ($item > 10 && $item % 2 === 0) {
echo "Even and greater than 10: $item\n";
}
}
}
This code checks each number in an array to see if it is even and greater than 10.
- Primary operation: Looping through each item in the array.
- How many times: Once for every element in the input array.
As the array gets bigger, the code checks more items one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input size grows.
[X] Wrong: "Logical operators make the code run slower exponentially."
[OK] Correct: Logical operators just combine conditions and run once per item, so they don't multiply the work by input size.
Understanding how logical checks scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we added a nested loop inside the foreach? How would the time complexity change?"
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
