Comparison operators (loose and strict) in PHP - Time & Space Complexity
We want to understand how the time it takes to compare values changes as we compare more items.
How does using loose or strict comparison affect the work done when checking many values?
Analyze the time complexity of the following code snippet.
$values = [1, '1', 2, '2', 3, '3'];
$target = 2;
foreach ($values as $value) {
if ($value == $target) { // loose comparison
echo "$value matches loosely\n";
}
if ($value === $target) { // strict comparison
echo "$value matches strictly\n";
}
}
This code checks each value in an array against a target using both loose and strict comparisons.
- Primary operation: Looping through each element in the array and comparing it to the target.
- How many times: Once for each element in the array.
As the number of values grows, the number of comparisons grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The work grows directly with the number of items; doubling items doubles the comparisons.
Time Complexity: O(n)
This means the time to compare grows in a straight line with the number of values checked.
[X] Wrong: "Strict comparison takes more time than loose comparison because it checks types too."
[OK] Correct: Both comparisons happen once per item, so the overall time grows the same way; the difference in checking type is very small and does not change the growth pattern.
Understanding how comparison operations scale helps you explain how your code behaves with bigger data, a skill that shows you think about efficiency clearly.
"What if we changed the array to a nested array and compared inner arrays? How would the time complexity change?"
Practice
Solution
Step 1: Understand loose vs strict comparison
Loose comparison (==) checks value after type conversion, strict (===) checks value and type exactly.Step 2: Identify operator that checks type and value
The operator === compares both type and value strictly without conversion.Final Answer:
=== -> Option AQuick Check:
Strict comparison = === [OK]
- Confusing == with ===
- Thinking != checks type
- Using <> instead of !=
Solution
Step 1: Recall PHP strict inequality operator
Strict inequality operator in PHP is !== which checks both type and value are not equal.Step 2: Identify correct syntax
!== is valid syntax; !=== is invalid and others are loose inequality.Final Answer:
!== -> Option BQuick Check:
Strict inequality = !== [OK]
- Using !=== which is invalid
- Confusing != with !==
- Using <> for strict inequality
$a = 0;
$b = '0';
var_dump($a == $b);
var_dump($a === $b);
Solution
Step 1: Evaluate loose comparison ($a == $b)
0 (int) and '0' (string) are equal after type conversion, so $a == $b is true.Step 2: Evaluate strict comparison ($a === $b)
Types differ (int vs string), so $a === $b is false.Final Answer:
bool(true) bool(false) -> Option AQuick Check:
Loose == true, Strict === false [OK]
- Assuming both comparisons return true
- Ignoring type difference in strict check
- Confusing output order
$x = 5;
if ($x = '5') {
echo 'Equal';
} else {
echo 'Not equal';
}
Solution
Step 1: Identify operator in if condition
The condition uses = which assigns '5' to $x instead of comparing.Step 2: Understand effect of assignment in condition
Assignment returns the assigned value, which is truthy, so 'Equal' always prints regardless of original $x.Final Answer:
Assignment (=) used instead of comparison (==) -> Option CQuick Check:
Use == for comparison, not = [OK]
- Confusing = with == in conditions
- Thinking strict comparison is error here
- Ignoring that assignment returns value
Solution
Step 1: Understand filtering requirement
We want to keep only elements that are integer 0, not string '0'. So type matters.Step 2: Choose operator that checks type and value
Strict equality (===) checks both value and type, so it excludes string '0'.Final Answer:
=== -> Option DQuick Check:
Strict equality === filters exact type and value [OK]
- Using == which allows type conversion
- Using != or !== which exclude 0
- Ignoring type difference in filtering
Ready to prove it?
Pass the test and this pattern moves unknown
