Bird
0
0

Identify the error in this PHP code:

medium📝 Debug Q7 of 15
PHP - Conditional Statements
Identify the error in this PHP code:
if ($num = 5) {
echo 'Equal';
}
AVariable $num is undefined
BMissing semicolon after if
CUsing assignment instead of comparison
DCurly braces are incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check the condition inside if

    The code uses single equals (=) which assigns 5 to $num instead of comparing.
  2. Step 2: Understand the impact

    Assignment returns true, so the if block always runs, which is likely a mistake.
  3. Final Answer:

    Using assignment instead of comparison -> Option C
  4. Quick Check:

    Use '==' for comparison, '=' is assignment [OK]
Quick Trick: Use '==' to compare, '=' assigns value [OK]
Common Mistakes:
  • Confusing '=' with '==' in conditions
  • Expecting condition to check equality
  • Ignoring that assignment returns true

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes