0
0
PHPprogramming~20 mins

For loop execution model in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple for loop with break
What is the output of this PHP code?
PHP
<?php
for ($i = 0; $i < 5; $i++) {
    if ($i == 3) {
        break;
    }
    echo $i . ' ';
}
?>
A0 1 2
B0 1 2 3
C0 1 2 3 4
D1 2 3
Attempts:
2 left
💡 Hint
The loop stops when the condition inside the if is true.
Predict Output
intermediate
2:00remaining
For loop with continue statement
What will this PHP code output?
PHP
<?php
for ($i = 0; $i < 5; $i++) {
    if ($i % 2 == 0) {
        continue;
    }
    echo $i . ' ';
}
?>
A1 3
B0 2 4
C1 3 5
D0 1 2 3 4
Attempts:
2 left
💡 Hint
The continue skips the current loop iteration when the number is even.
Predict Output
advanced
2:00remaining
For loop with multiple expressions in control
What is the output of this PHP code?
PHP
<?php
for ($i = 0, $j = 5; $i < 3; $i++, $j--) {
    echo $i . '-' . $j . ' ';
}
?>
A0-5 1-4
B0-5 1-4 2-3 3-2
C0-5 1-4 2-3
D5-0 4-1 3-2
Attempts:
2 left
💡 Hint
Both variables change each loop iteration.
Predict Output
advanced
2:00remaining
For loop with missing increment expression
What will this PHP code output?
PHP
<?php
for ($i = 0; $i < 3;) {
    echo $i . ' ';
    $i++;
}
?>
A0 1 2 3
B0 1 2
CInfinite loop (no output ends)
DSyntax error
Attempts:
2 left
💡 Hint
Increment can be inside the loop body.
🧠 Conceptual
expert
2:00remaining
Understanding for loop execution order
In PHP, what is the correct order of execution for the parts of a for loop?
A3,1,2,4
B2,1,3,4
C1,3,2,4
D1,2,3,4
Attempts:
2 left
💡 Hint
Initialization happens once, then condition is checked before each loop.