Bird
0
0

Identify the syntax error in this PHP while loop:

medium📝 Debug Q6 of 15
PHP - Loops

Identify the syntax error in this PHP while loop:

$count = 1;
while ($count <= 5)
    echo $count;
    $count++;
AMissing curly braces causing incorrect loop body
BIncorrect variable name used inside loop
CCondition uses wrong comparison operator
DLoop variable is not initialized
Step-by-Step Solution
Solution:
  1. Step 1: Analyze loop structure

    The loop lacks curly braces, so only the first statement is inside the loop.
  2. Step 2: Effect of missing braces

    $count++ is outside the loop, causing an infinite loop.
  3. Final Answer:

    Missing curly braces causing incorrect loop body -> Option A
  4. Quick Check:

    Check if all intended statements are inside braces [OK]
Quick Trick: Always use braces to define loop body clearly [OK]
Common Mistakes:
  • Assuming indentation defines loop body
  • Forgetting to increment loop variable inside loop
  • Misplacing semicolons

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes