A for loop is used to execute a block of code
repeatedly. It is especially useful when you know how many
times you want to repeat an operation.
A for loop contains initialization, a condition, and an update expression.
Syntax:
for (initialization; condition; update) {
// Code to repeat
}
Example:
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . "<br>";
}
?>
Output:
1
2
3
4
5
The initialization runs first. PHP then checks the condition. If it is true, the loop body runs, followed by the update expression.
<?php
for ($i = 1; $i <= 3; $i++) {
echo "Number: " . $i . "<br>";
}
?>
Output:
Number: 1
Number: 2
Number: 3
After the third iteration, $i becomes 4.
The condition is false, so the loop stops.
You can print numbers in descending order by decreasing the counter with the decrement operator.
<?php
for ($i = 10; $i >= 1; $i--) {
echo $i . "<br>";
}
?>
Output:
10
9
8
7
6
5
4
3
2
1
The loop begins at 10 and subtracts one after each iteration until the counter reaches 0.
The modulus operator (%) returns
the remainder after division. An even integer
has a remainder of zero when divided by 2.
Example: Even numbers
<?php
for ($i = 2; $i <= 20; $i += 2) {
echo $i . "<br>";
}
?>
Output:
2
4
6
8
10
12
14
16
18
20
Example: Odd numbers
<?php
for ($i = 1; $i <= 10; $i += 2) {
echo $i . "<br>";
}
?>
Output:
1
3
5
7
9
A for loop can add a sequence of numbers and store the total in a variable.
<?php
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
}
echo "Sum = " . $sum;
?>
Output:
Sum = 55
The loop adds every integer from 1 through 10
to the $sum variable.
A for loop makes it easy to generate a multiplication table.
<?php
$number = 6;
for ($i = 1; $i <= 10; $i++) {
echo $number . " x " . $i . " = ";
echo ($number * $i) . "<br>";
}
?>
Output:
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
Change $number = 6 to another number
to generate a different multiplication table.
A nested for loop is a for loop inside another for loop. It is useful for patterns and tables.
<?php
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
echo "* ";
}
echo "<br>";
}
?>
Output:
* * *
* * *
* * *
The outer loop controls the rows, while the inner loop controls the stars in each row.
You can use a for loop to access indexed array elements using their positions.
<?php
$fruits = ["Apple", "Banana", "Mango", "Orange"];
for ($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i] . "<br>";
}
?>
Output:
Apple
Banana
Mango
Orange
The first array element is at index 0. The condition ensures that the loop stops when all elements have been displayed.
The break statement exits the loop.
The continue statement skips the
remaining statements in the current iteration.
Example using break:
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break;
}
echo $i . "<br>";
}
?>
Output:
1
2
3
4
Example using continue:
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i . "<br>";
}
?>
Output:
1
2
4
5
The first loop stops when the counter reaches 5. The second loop skips 3 and displays the other numbers.
Write PHP programs to complete these exercises:
Sample starting code:
<?php
$sum = 0;
for ($i = 1; $i <= 100; $i++) {
$sum += $i;
}
echo "Total = " . $sum;
?>
Expected output:
Total = 5050
1. Which part of a for loop executes only once at the beginning?
2. What is the output of this code?
for ($i = 1; $i <= 3; $i++) {
echo $i;
}
3. Which statement immediately exits a loop?