Bird
Raised Fist0
PHPprogramming~10 mins

Loose comparison vs strict comparison in PHP - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Loose comparison vs strict comparison
Start
↓
Compare two values
↓
Loose comparison (==)
↓
Result: true or false
↓
Strict comparison (===)
↓
Result: true or false
The program compares two values either loosely (==) allowing type conversion, or strictly (===) requiring same type and value.
Execution Sample
PHP
<?php
$a = 5;
$b = '5';
var_dump($a == $b);
var_dump($a === $b);
?>
Compares integer 5 and string '5' using loose and strict comparison, showing different results.
Execution Table
StepExpressionType BeforeType AfterComparison TypeResult
1$a == $bint (5), string ('5')int (5), int (5)Loose (==)true
2$a === $bint (5), string ('5')int (5), string ('5')Strict (===)false
💡 Both comparisons done, results shown: loose is true due to type juggling, strict is false due to type difference.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$aint 5int 5int 5int 5
$bstring '5'string '5'string '5'string '5'
Key Moments - 2 Insights
Why does loose comparison ($a == $b) return true even though types differ?
Loose comparison converts types to match before comparing, so string '5' becomes int 5, making values equal (see execution_table step 1).
Why does strict comparison ($a === $b) return false?
Strict comparison checks both value and type without conversion. Since $a is int and $b is string, they are not identical (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of $a == $b at step 1?
Atrue
Bfalse
Cnull
Derror
💡 Hint
Check the 'Result' column for step 1 in the execution_table.
At which step does the comparison consider type differences without conversion?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
Look at the 'Comparison Type' column to see which uses strict comparison.
If $b was integer 5 instead of string '5', what would $a === $b return?
Anull
Bfalse
Ctrue
Derror
💡 Hint
Refer to variable_tracker to see types; strict comparison requires same type and value.
Concept Snapshot
Loose comparison (==) checks if values are equal after converting types.
Strict comparison (===) checks if values and types are exactly the same.
Use loose when type conversion is okay.
Use strict to avoid unexpected matches.
Loose can cause surprises with different types.
Strict is safer for exact matches.
Full Transcript
This example compares two variables $a and $b in PHP. $a is an integer 5, $b is a string '5'. First, it uses loose comparison (==) which converts the string to integer before comparing, so it returns true. Then it uses strict comparison (===) which checks both value and type without conversion, so it returns false because one is int and the other is string. This shows how loose comparison can consider different types equal if values match, while strict comparison requires exact type and value match.

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

  1. Step 1: Understand loose comparison

    Loose comparison (==) converts types if needed and then compares values.
  2. Step 2: Understand strict comparison

    Strict comparison (===) checks both value and type without conversion.
  3. Final Answer:

    == compares values after type conversion, === compares value and type exactly -> Option A
  4. 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

  1. Step 1: Identify strict comparison operator

    Strict comparison uses three equal signs: ===.
  2. Step 2: Check syntax correctness

    $a === $b; is valid syntax for strict comparison.
  3. Final Answer:

    $a === $b; -> Option D
  4. 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 = 0;
$b = '0';
if ($a == $b) {
echo 'Equal';
} else {
echo 'Not Equal';
}
if ($a === $b) {
echo ' Strictly Equal';
} else {
echo ' Not Strictly Equal';
}
medium
A. Equal Strictly Equal
B. Not Equal Strictly Equal
C. Equal Not Strictly Equal
D. Not Equal Not Strictly Equal

Solution

  1. Step 1: Evaluate loose comparison ($a == $b)

    $a is integer 0, $b is string '0'. Loose comparison converts types, so 0 == '0' is true, prints 'Equal'.
  2. Step 2: Evaluate strict comparison ($a === $b)

    Strict comparison checks type and value. Integer 0 !== string '0', so false, prints ' Not Strictly Equal'.
  3. Final Answer:

    Equal Not Strictly Equal -> Option C
  4. Quick Check:

    0 == '0' true, 0 === '0' false = Equal Not Strictly Equal [OK]
Hint: Loose ignores type, strict checks type too [OK]
Common Mistakes:
  • Assuming strict comparison returns true for 0 and '0'
  • Confusing output order
  • Ignoring spaces in echoed strings
4. Find the error in this PHP code snippet:
$x = '10';
if ($x = 10) {
echo 'Match';
} else {
echo 'No Match';
}
medium
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

  1. Step 1: Check if condition operator

    The condition uses = which assigns 10 to $x instead of comparing.
  2. Step 2: Understand effect of assignment in if

    Assignment returns 10 (truthy), so 'Match' always prints, ignoring original $x value.
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in if condition -> Option A
  4. 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

  1. Step 1: Understand the requirement

    We want to check if $input is integer 0 exactly, not string '0' or other falsy values.
  2. Step 2: Choose strict comparison

    == allows type conversion, so string '0' == 0 is true, which is not desired. === checks type and value strictly.
  3. Final Answer:

    if ($input === 0) -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard