Loose comparison uses the == operator and compares values after converting them to a common type. It checks if values are equal but ignores their data types.
Click to reveal answer
beginner
What is strict comparison in PHP?
Strict comparison uses the === operator and compares both the value and the data type. Both must be exactly the same for the comparison to be true.
Click to reveal answer
intermediate
What will be the result of: 0 == '0' and 0 === '0' in PHP?
0 == '0' is true because loose comparison converts '0' to integer 0. 0 === '0' is false because strict comparison checks type and integer is not string.
Click to reveal answer
intermediate
Why can loose comparison lead to unexpected results?
Because it converts types automatically, values that look different can be treated as equal, which may cause bugs if you expect exact matches.
Click to reveal answer
beginner
When should you use strict comparison over loose comparison?
Use strict comparison when you want to be sure both value and type match exactly, such as checking user input or sensitive data.
Click to reveal answer
Which operator in PHP performs strict comparison?
A===
B!=
C==
D!==
✗ Incorrect
The === operator checks both value and type, making it a strict comparison.
What does loose comparison do before comparing values?
AChecks only data types
BThrows an error
CConverts values to a common type
DIgnores values
✗ Incorrect
Loose comparison converts values to a common type before comparing.
What is the result of '5' == 5 in PHP?
Anull
Bfalse
Cerror
Dtrue
✗ Incorrect
Loose comparison converts '5' to integer 5, so they are equal.
What is the result of '5' === 5 in PHP?
Atrue
Bfalse
Cerror
Dnull
✗ Incorrect
Strict comparison checks type, string is not integer, so false.
Which comparison is safer to avoid unexpected bugs?
AStrict comparison (===)
BLoose comparison (==)
CBoth are equally safe
DNeither is safe
✗ Incorrect
Strict comparison avoids type juggling and unexpected matches.
Explain the difference between loose and strict comparison in PHP.
Think about how PHP treats types in each comparison.
You got /4 concepts.
Give an example where loose comparison can cause a bug and how strict comparison fixes it.
Consider comparing a number and a string.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between == and === in PHP?
easy
A. == compares values after type conversion, === compares value and type exactly
B. == compares only types, === compares only values
C. == is used for assignment, === is used for comparison
D. == compares memory addresses, === compares values
Solution
Step 1: Understand loose comparison
Loose comparison (==) converts types if needed and then compares values.
Step 2: Understand strict comparison
Strict comparison (===) checks both value and type without conversion.
Final Answer:
== compares values after type conversion, === compares value and type exactly -> Option A
Quick Check:
Loose == vs Strict === = == compares values after type conversion, === compares value and type exactly [OK]
Hint: Remember: === means exactly equal type and value [OK]
Common Mistakes:
Thinking == compares types only
Confusing assignment (=) with comparison (==)
Assuming === converts types before comparing
2. Which of the following is the correct syntax for strict comparison in PHP?
easy
A. $a = $b;
B. $a !== $b;
C. $a == $b;
D. $a === $b;
Solution
Step 1: Identify strict comparison operator
Strict comparison uses three equal signs: ===.
Step 2: Check syntax correctness
$a === $b; is valid syntax for strict comparison.
Final Answer:
$a === $b; -> Option D
Quick Check:
Strict comparison syntax = $a === $b; [OK]
Hint: Use three equal signs for strict comparison [OK]
Common Mistakes:
Typing == instead of ===
Using = which is assignment, not comparison
Confusing == (loose) with === (strict)
3. What will be the output of the following PHP code?
A. Using assignment (=) instead of comparison (==) in if condition
B. Missing semicolon after echo statement
C. Using double quotes instead of single quotes
D. Variable $x is not defined
Solution
Step 1: Check if condition operator
The condition uses = which assigns 10 to $x instead of comparing.
Step 2: Understand effect of assignment in if
Assignment returns 10 (truthy), so 'Match' always prints, ignoring original $x value.
Final Answer:
Using assignment (=) instead of comparison (==) in if condition -> Option A
Quick Check:
Assignment in if condition = Using assignment (=) instead of comparison (==) in if condition [OK]
Hint: Use == or === for comparison, not = [OK]
Common Mistakes:
Confusing = with == in conditions
Thinking missing semicolon causes error here
Assuming quotes affect comparison operator
5. You want to check if a user input variable $input is exactly the integer 0 (zero) and not a string '0' or other falsy value. Which condition is the best to use?
hard
A. if ($input == 0)
B. if ($input === 0)
C. if ($input != 0)
D. if ($input !== 0)
Solution
Step 1: Understand the requirement
We want to check if $input is integer 0 exactly, not string '0' or other falsy values.
Step 2: Choose strict comparison
== allows type conversion, so string '0' == 0 is true, which is not desired. === checks type and value strictly.
Final Answer:
if ($input === 0) -> Option B
Quick Check:
Exact type and value check = if ($input === 0) [OK]
Hint: Use === to check exact type and value [OK]
Common Mistakes:
Using == and getting true for string '0'
Using != or !== which check for inequality
Ignoring type differences in comparisons
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.