Skip to content

PHP For Loop

Syntax

The for loop in PHP is used to iterate through a block of code a predetermined number of times, making it ideal for scenarios where the iteration count is known.

// Generic version of a for loop
for (initialization; condition; increment) {
// Code to execute in each iteration
}

Example

// Concrete example of a for loop
for ($i = 1; $i <= 5; $i++) {
echo "$i ";
}
// Output: 1 2 3 4 5