Bird
Raised Fist0
PHPprogramming~10 mins

Comparison operators (loose and strict) in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Comparison operators (loose and strict)
Start
↓
Evaluate Left Operand
↓
Evaluate Right Operand
↓
Choose Operator Type
↓
Convert Types if Needed
↓
Compare Values
↓
Return True/False
↓
Check Type and Value
↓
Return True/False
↓
End
The program compares two values either loosely (==) by converting types or strictly (===) by checking type and value exactly.
Execution Sample
PHP
<?php
$a = 5;
$b = '5';
var_dump($a == $b);
var_dump($a === $b);
?>
This code compares an integer 5 and a string '5' using loose and strict comparison and prints the results.
Execution Table
StepExpressionOperationResultExplanation
1$a == $bLoose comparisontrue5 (int) and '5' (string) converted to same type, values equal
2$a === $bStrict comparisonfalseTypes differ (int vs string), so result is false
💡 Both comparisons evaluated; loose returns true due to type conversion, strict returns false due to type mismatch.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$a5 (int)5 (int)5 (int)5 (int)
$b'5' (string)'5' (string)'5' (string)'5' (string)
Key Moments - 2 Insights
Why does '$a == $b' return true even though one is int and the other is string?
Because loose comparison (==) converts types to match before comparing values, so '5' string becomes 5 integer, making them equal (see execution_table step 1).
Why does '$a === $b' return false?
Strict comparison (===) checks both type and value without conversion. Since $a is int and $b is string, they are not strictly equal (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of '$a == $b'?
Atrue
Bfalse
Cnull
Derror
💡 Hint
Check execution_table row 1 under Result column.
At which step does the comparison check type without conversion?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
See execution_table step 2 Operation column describing 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 showing types and strict comparison rules.
Concept Snapshot
Comparison operators in PHP:
- Loose (==): converts types, compares values.
- Strict (===): compares type and value exactly.
- Use === to avoid unexpected true from type juggling.
- Example: 5 == '5' is true, 5 === '5' is false.
Full Transcript
This example shows how PHP compares values using loose and strict operators. First, it compares integer 5 and string '5' with loose equality (==), which converts the string to integer and finds them equal, returning true. Then it compares the same values with strict equality (===), which checks type and value without conversion, so it returns false because one is int and the other is string. This teaches that loose comparison can lead to unexpected true results due to type juggling, while strict comparison is safer for exact matches.

Practice

(1/5)
1. Which PHP comparison operator checks both value and type strictly?
easy
A. ===
B. ==
C. !=
D. <>

Solution

  1. Step 1: Understand loose vs strict comparison

    Loose comparison (==) checks value after type conversion, strict (===) checks value and type exactly.
  2. Step 2: Identify operator that checks type and value

    The operator === compares both type and value strictly without conversion.
  3. Final Answer:

    === -> Option A
  4. Quick 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
A. !=
B. !==
C. <>
D. !===

Solution

  1. Step 1: Recall PHP strict inequality operator

    Strict inequality operator in PHP is !== which checks both type and value are not equal.
  2. Step 2: Identify correct syntax

    !== is valid syntax; !=== is invalid and others are loose inequality.
  3. Final Answer:

    !== -> Option B
  4. Quick 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
A. bool(true) bool(false)
B. bool(false) bool(true)
C. bool(true) bool(true)
D. bool(false) bool(false)

Solution

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

    0 (int) and '0' (string) are equal after type conversion, so $a == $b is true.
  2. Step 2: Evaluate strict comparison ($a === $b)

    Types differ (int vs string), so $a === $b is false.
  3. Final Answer:

    bool(true) bool(false) -> Option A
  4. Quick 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
A. Missing semicolon after echo
B. Using strict comparison (===) instead of loose
C. Assignment (=) used instead of comparison (==)
D. Variable $x is not defined

Solution

  1. Step 1: Identify operator in if condition

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

    Assignment returns the assigned value, which is truthy, so 'Equal' always prints regardless of original $x.
  3. Final Answer:

    Assignment (=) used instead of comparison (==) -> Option C
  4. Quick 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
A. ==
B. !==
C. !=
D. ===

Solution

  1. Step 1: Understand filtering requirement

    We want to keep only elements that are integer 0, not string '0'. So type matters.
  2. Step 2: Choose operator that checks type and value

    Strict equality (===) checks both value and type, so it excludes string '0'.
  3. Final Answer:

    === -> Option D
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard