Bird
0
0

Which of the following is the correct syntax for a PHP for loop that counts from 0 to 4?

easy📝 Syntax Q12 of 15
PHP - Loops
Which of the following is the correct syntax for a PHP for loop that counts from 0 to 4?
Afor $i = 0; $i <= 4; $i++ {}
Bfor ($i = 0; $i < 4; $i--) {}
Cfor ($i = 0; $i <= 4; ++i) {}
Dfor ($i = 0; $i <= 4; $i++) {}
Step-by-Step Solution
Solution:
  1. Step 1: Check for loop syntax rules

    PHP for loops require parentheses around initialization, condition, and increment, separated by semicolons.
  2. Step 2: Validate each option

    for ($i = 0; $i <= 4; $i++) {} uses correct syntax with $i starting at 0, condition $i <= 4, and increment $i++. Others have syntax errors or wrong increments.
  3. Final Answer:

    for ($i = 0; $i <= 4; $i++) {} -> Option D
  4. Quick Check:

    Correct for loop syntax = for ($i = 0; $i <= 4; $i++) {} [OK]
Quick Trick: Use parentheses and semicolons correctly in for loops [OK]
Common Mistakes:
  • Missing parentheses around for loop parts
  • Using ++i without $ sign
  • Wrong increment direction (i-- instead of i++)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes