Bird
0
0

What will this PHP code print?

medium📝 Predict Output Q5 of 15
PHP - Loops
What will this PHP code print?
for ($x = 0; $x < 3; $x++) {
for ($y = 0; $y < 2; $y++) {
echo $x + $y . ' ';
}
}
A0 1 0 1 0 1
B1 2 3 4 5 6
C0 1 1 2 2 3
D0 0 1 1 2 2
Step-by-Step Solution
Solution:
  1. Step 1: Calculate sums for each loop iteration

    For $x=0: $y=0 sum=0, $y=1 sum=1; For $x=1: sums 1 and 2; For $x=2: sums 2 and 3.
  2. Step 2: Combine all outputs in order

    Outputs are: 0 1 1 2 2 3 with spaces.
  3. Final Answer:

    0 1 1 2 2 3 -> Option C
  4. Quick Check:

    Sum of loop vars printed in order = 0 1 1 2 2 3 [OK]
Quick Trick: Add outer and inner loop variables for output [OK]
Common Mistakes:
  • Confusing loop variable values
  • Printing variables without addition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes