Bird
0
0

Find the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Operators
Find the error in this PHP code snippet:
$x = 5;
if ($x = '5') {
echo 'Equal';
} else {
echo 'Not equal';
}
AMissing semicolon after echo
BUsing strict comparison (===) instead of loose
CAssignment (=) used instead of comparison (==)
DVariable $x is not defined
Step-by-Step Solution
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]
Quick Trick: Use == for comparison, not = assignment [OK]
Common Mistakes:
  • Confusing = with == in conditions
  • Thinking strict comparison is error here
  • Ignoring that assignment returns value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes