Recall & Review
beginner
What are the three main parts of a
for loop in PHP?The three main parts are:<br>1. Initialization (runs once at the start)<br>2. Condition (checked before each loop iteration)<br>3. Increment/Decrement (runs after each iteration)
Click to reveal answer
beginner
Explain the order of execution in a PHP
for loop.First, the initialization runs once.<br>Then, the condition is checked.<br>If true, the loop body runs.<br>After the body, the increment/decrement runs.<br>This repeats until the condition is false.
Click to reveal answer
beginner
What happens if the condition in a
for loop is false at the start?The loop body does not run at all.<br>The program skips the loop and continues after it.
Click to reveal answer
intermediate
Can the increment part of a
for loop be empty in PHP? What does that mean?Yes, it can be empty.<br>This means no automatic change happens after each iteration.<br>You must manually change the loop variable inside the loop body to avoid infinite loops.
Click to reveal answer
beginner
Show a simple PHP
for loop that prints numbers 1 to 5.Example:<br>
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . "\n";
}
?><br>This prints numbers 1, 2, 3, 4, 5 each on a new line.Click to reveal answer
In a PHP
for loop, which part runs only once at the start?✗ Incorrect
Initialization runs once before the loop starts to set up the loop variable.
What happens if the condition in a PHP
for loop is false initially?✗ Incorrect
If the condition is false at the start, the loop body does not execute at all.
Which part of the
for loop runs after each iteration?✗ Incorrect
The increment part runs after each loop iteration to update the loop variable.
Is it possible to have an empty increment section in a PHP
for loop?✗ Incorrect
You can leave increment empty but must update the loop variable inside the loop to avoid infinite loops.
What is the correct syntax for a PHP
for loop that counts from 0 to 4?✗ Incorrect
Option D correctly counts from 0 up to 4 by incrementing $i until it is less than 5.
Describe the execution flow of a PHP
for loop from start to finish.Think about what happens before, during, and after each loop cycle.
You got /5 concepts.
Explain what happens if you forget to update the loop variable inside a
for loop with an empty increment section.Consider what keeps the loop going or stopping.
You got /4 concepts.