0
0
PHPprogramming~5 mins

For loop execution model in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ACondition
BInitialization
CIncrement
DLoop body
What happens if the condition in a PHP for loop is false initially?
AThe loop body does not run
BThe loop runs infinitely
CThe increment runs once
DThe loop runs once
Which part of the for loop runs after each iteration?
AIncrement
BCondition
CInitialization
DLoop body
Is it possible to have an empty increment section in a PHP for loop?
AYes, and the loop will run infinitely
BNo, it is required
CYes, but you must update the variable inside the loop
DNo, it causes a syntax error
What is the correct syntax for a PHP for loop that counts from 0 to 4?
Afor ($i = 5; $i > 0; $i--)
Bfor ($i = 1; $i <= 5; $i++)
Cfor ($i = 0; $i <= 5; $i--)
Dfor ($i = 0; $i < 5; $i++)
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.