Are you sure your comparisons are really checking what you think they are?
Why Comparison operators (loose and strict) in PHP? - Purpose & Use Cases
Imagine you have a list of user inputs and you want to check if each input matches a stored password. You try to compare them manually by just looking at the values or using simple checks without understanding how PHP compares different types.
Doing this manually is slow and confusing because PHP can treat values like strings and numbers differently. Sometimes '123' and 123 look the same but PHP might say they are equal or not depending on how you compare them. This causes bugs and security holes if you don't check carefully.
Using PHP's loose (==) and strict (===) comparison operators helps you clearly decide if you want to compare values only or both value and type. This makes your code safer and easier to understand, avoiding unexpected results.
$a = '123'; $b = 123; if ($a == $b) { echo 'Equal'; }
$a = '123'; $b = 123; if ($a === $b) { echo 'Equal'; } else { echo 'Not equal'; }
You can write clear and bug-free code that correctly checks if values are truly the same, including their type.
When checking user passwords or tokens, strict comparison ensures that '123' (string) is not confused with 123 (number), preventing unauthorized access.
Loose comparison (==) checks value only, ignoring type.
Strict comparison (===) checks both value and type.
Choosing the right operator prevents bugs and security issues.
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
