Challenge - 5 Problems
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple for loop in PHP
What will be the output of this PHP code?
PHP
<?php for ($i = 1; $i <= 3; $i++) { echo $i . " "; } ?>
Attempts:
2 left
💡 Hint
Look at how the loop counts from 1 to 3 and prints each number with a space.
✗ Incorrect
The for loop starts at 1 and runs while $i is less than or equal to 3, printing each number followed by a space.
🧠 Conceptual
intermediate1:30remaining
Why use loops instead of repeating code?
Why do programmers use loops instead of writing the same code multiple times?
Attempts:
2 left
💡 Hint
Think about what happens if you want to change repeated code.
✗ Incorrect
Loops help avoid repeating code, making programs shorter and easier to update or fix.
🔧 Debug
advanced2:00remaining
Identify the error in this while loop
What error will this PHP code cause?
PHP
<?php $i = 0; while ($i < 3) { echo $i . " "; } ?>
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
✗ Incorrect
The variable $i is never increased, so the condition is always true, causing an infinite loop.
❓ Predict Output
advanced2:00remaining
Output of nested loops in PHP
What will this PHP code output?
PHP
<?php for ($i = 1; $i <= 2; $i++) { for ($j = 1; $j <= 2; $j++) { echo $i . $j . " "; } } ?>
Attempts:
2 left
💡 Hint
The outer loop controls $i, inner loop controls $j, both run from 1 to 2.
✗ Incorrect
The outer loop runs twice, and for each $i, the inner loop runs twice, printing combinations of $i and $j.
🧠 Conceptual
expert2:30remaining
Why loops are essential for processing lists
Which statement best explains why loops are essential when working with lists or arrays in PHP?
Attempts:
2 left
💡 Hint
Think about how you handle many items in a list.
✗ Incorrect
Loops let you repeat actions on every item in a list easily, avoiding repetitive code.