Comparison operators help you check if two values are equal or different. Loose and strict comparisons decide how carefully PHP checks the values.
Comparison operators (loose and strict) in PHP
Introduction
Syntax
PHP
$a == $b // loose equality $a === $b // strict equality $a != $b // loose inequality $a !== $b // strict inequality
Loose comparison (==, !=) converts types if needed before comparing.
Strict comparison (===, !==) checks both value and type without conversion.
Examples
PHP
$a = 5; $b = '5'; var_dump($a == $b); // true var_dump($a === $b); // false
PHP
$x = 0; $y = false; var_dump($x == $y); // true var_dump($x === $y); // false
PHP
$m = null; $n = ''; var_dump($m == $n); // false var_dump($m === $n); // false
Sample Program
This program compares variables using loose and strict operators and prints which comparisons are true or false.
PHP
<?php $a = 10; $b = '10'; $c = 20; if ($a == $b) { echo "$a == $b is true\n"; } else { echo "$a == $b is false\n"; } if ($a === $b) { echo "$a === $b is true\n"; } else { echo "$a === $b is false\n"; } if ($a != $c) { echo "$a != $c is true\n"; } else { echo "$a != $c is false\n"; } if ($a !== $b) { echo "$a !== $b is true\n"; } else { echo "$a !== $b is false\n"; } ?>
Important Notes
Loose comparison can cause unexpected results if types differ, so use strict comparison when type matters.
Strict comparison is safer for checking exact matches, like passwords or IDs.
Summary
Use == and != for loose comparison that converts types.
Use === and !== for strict comparison that checks type and value.
Strict comparison helps avoid bugs from type juggling.
Practice
1. Which PHP comparison operator checks both value and type strictly?
easy
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]
Hint: Use === to compare type and value exactly [OK]
Common Mistakes:
- Confusing == with ===
- Thinking != checks type
- Using <> instead of !=
2. Which of the following is the correct syntax for a strict inequality check in PHP?
easy
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]
Hint: Use !== for strict not equal check [OK]
Common Mistakes:
- Using !=== which is invalid
- Confusing != with !==
- Using <> for strict inequality
3. What will be the output of this PHP code?
$a = 0;
$b = '0';
var_dump($a == $b);
var_dump($a === $b);
medium
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]
Hint: Remember === checks type too, == converts types [OK]
Common Mistakes:
- Assuming both comparisons return true
- Ignoring type difference in strict check
- Confusing output order
4. Find the error in this PHP code snippet:
$x = 5;
if ($x = '5') {
echo 'Equal';
} else {
echo 'Not equal';
}
medium
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]
Hint: Use == for comparison, not = assignment [OK]
Common Mistakes:
- Confusing = with == in conditions
- Thinking strict comparison is error here
- Ignoring that assignment returns value
5. You want to filter an array to keep only elements that are exactly integer 0, excluding string '0'. Which comparison operator should you use in PHP?
hard
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]
Hint: Use === to match exact type and value [OK]
Common Mistakes:
- Using == which allows type conversion
- Using != or !== which exclude 0
- Ignoring type difference in filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
