Bird
0
0

Which of the following is the correct syntax for a nested for loop in PHP?

easy📝 Syntax Q12 of 15
PHP - Loops
Which of the following is the correct syntax for a nested for loop in PHP?
Afor ($i = 0; $i < 2; $i++) { for $j = 0; $j < 3; $j++ { echo $i . $j; } }
Bfor ($i = 0; $i < 2; $i++) { for ($j = 0; $j < 3; $j++) { echo $i . $j; } }
Cfor ($i = 0; $i < 2; $i++) for ($j = 0; $j < 3; $j++) echo $i . $j;
Dfor $i = 0; $i < 2; $i++ { for $j = 0; $j < 3; $j++ { echo $i . $j; } }
Step-by-Step Solution
Solution:
  1. Step 1: Check PHP for loop syntax

    PHP requires parentheses around loop conditions and braces for blocks.
  2. Step 2: Validate nested loop syntax

    for ($i = 0; $i < 2; $i++) { for ($j = 0; $j < 3; $j++) { echo $i . $j; } } uses correct parentheses and braces for both loops and echo statement inside.
  3. Final Answer:

    for ($i = 0; $i < 2; $i++) { for ($j = 0; $j < 3; $j++) { echo $i . $j; } } -> Option B
  4. Quick Check:

    Correct parentheses and braces in nested loops [OK]
Quick Trick: Use parentheses and braces properly in PHP loops [OK]
Common Mistakes:
  • Omitting parentheses around loop conditions
  • Missing braces for loop blocks
  • Incorrect for loop syntax without $ sign

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes