PHP for Keyword
last modified November 25, 2026
The PHP for keyword starts a counted loop. Its header holds an initializer, a condition, and a step, which makes the whole life cycle of the counter visible in one line.
This article covers the order in which the three clauses run, comma expressions, counting down, omitted clauses, break and continue, float counters, nested loops, and walking an array by index.
Basic definitions
A for loop has three clauses separated by semicolons: an initializer, a condition, and a step. The initializer runs once before the loop; the condition is tested before every pass; the body runs when the condition is true; the step runs after the body and before the next test.
The syntax is for (init; condition; step) { ... }. All three clauses are optional, but the two semicolons are not. Each clause may contain several expressions separated by commas, which are evaluated left to right. A loop with an empty condition is infinite and needs a break to end.
Because the condition is tested first, a for loop may run zero times. That is the difference from the do-while loop, which always runs its body once. Use for when the number of passes or the step is known in advance, and a while loop when the loop ends on a condition that the body discovers.
A first for loop
The first example prints the numbers from 1 to 5 and then shows the value of the counter after the loop.
<?php
declare(strict_types=1);
for ($i = 1; $i <= 5; $i++) {
echo $i, $i < 5 ? ", " : "\n";
}
echo "after the loop: $i\n";
The initializer sets $i to 1, the condition allows values up to 5, and the step increments the counter after each pass. The echo statement prints the number and then either a separator or a newline. When the condition finally fails, $i is 6, and the variable keeps that value because the loop does not create its own scope.
$ php for_basic.php 1, 2, 3, 4, 5 after the loop: 6
The three clauses in detail
The initializer and the step can hold more than one expression. A common pattern moves two counters toward each other, which halves the number of passes.
<?php
declare(strict_types=1);
for ($i = 0, $j = 10; $i < $j; $i++, $j--) {
echo "i=$i j=$j\n";
}
for ($step = 10; $step > 0; $step -= 3) {
echo $step, " ";
}
echo "\n";
$i = 0;
for (; $i < 3; $i++) {
echo $i, " ";
}
echo "\n";
In the first loop $i++ and $j-- are one comma expression in the step clause; both run after every pass. The second loop uses a step of -3, and the third omits the initializer because $i was set earlier. An empty initializer or step simply does nothing in that clause, but an empty condition counts as true and produces an infinite loop.
$ php for_three_parts.php i=0 j=10 i=1 j=9 i=2 j=8 i=3 j=7 i=4 j=6 10 7 4 1 0 1 2
Counting backwards
A negative step counts down. The condition must match the direction, or the loop runs forever.
<?php
declare(strict_types=1);
for ($i = 10; $i >= 0; $i -= 2) {
echo $i, " ";
}
echo "\n";
for ($bytes = 1024; $bytes >= 128; $bytes /= 2) {
echo $bytes, " ";
}
echo "\n";
The first loop stops when $i reaches -2, but the body never sees that value because the condition is tested first. The second loop divides by 2 on every pass. Division always returns a float, so $bytes becomes 512.0, 256.0, and so on; echo prints them without the decimal part, but the type is no longer int.
$ php for_backwards.php 10 8 6 4 2 0 1024 512 256 128
Omitting clauses
An empty condition makes the loop infinite, so the body has to end it. The other clauses may be empty as well, and a semicolon immediately after the header creates a loop with an empty body.
<?php
declare(strict_types=1);
$i = 0;
for (;;) {
if ($i >= 3) {
break;
}
echo $i, " ";
$i++;
}
echo "\n";
for (; $i < 5; $i++) {
echo $i, " ";
}
echo "\n";
for ($j = 0; $j < 2; $j++);
echo "j=$j\n";
The first loop is the clearest form of an intentional infinite loop: it prints 0, 1, 2 and leaves through break. The second loop reuses $i and omits the initializer, which is useful when the counter has already been set up. The third one is easy to misread: the semicolon after the closing parenthesis is the entire body, so only the header runs and $j ends at 2. Prefer braces even for an empty body, or rewrite the loop as a calculation.
The break statement article explains how to leave an infinite loop from several places, including nested loops.
$ php for_omit.php 0 1 2 3 4 j=2
continue and break
continue skips the rest of the body but not the step, so the counter still advances. break leaves the loop immediately, and the step is not run again.
<?php
declare(strict_types=1);
for ($i = 0; $i < 6; $i++) {
if ($i % 2 === 0) {
continue;
}
echo $i, " ";
}
echo "\n";
$sum = 0;
for ($i = 1; $i <= 100; $i++) {
if ($i > 5) {
break;
}
$sum += $i;
}
echo "sum=$sum\n";
The first loop prints only odd numbers, because the even values reach continue and jump straight to $i++. This is a useful difference from a do-while loop, where continue jumps to the condition and can skip the increment. The second loop leaves at 6, so the sum is 1 + 2 + 3 + 4 + 5 = 15, and the values from 7 to 100 are never visited.
$ php for_continue.php 1 3 5 sum=15
Floats as loop counters
A float step looks convenient but accumulates representation error. The next example adds 0.1 on every pass and never reaches the value 0.3.
<?php
declare(strict_types=1);
for ($price = 0.0; $price <= 0.3; $price += 0.1) {
printf("%.1f -> %.17f\n", $price, $price);
}
echo "after loop: ", $price, "\n";
for ($tenth = 1; $tenth <= 3; $tenth++) {
printf("%.1f\n", $tenth / 10);
}
The stored value after three additions is slightly greater than 0.3, so the condition $price <= 0.3 fails and the last price is never printed. Printing with %.17f shows the actual stored digits. Counting with an integer and dividing inside the body avoids the drift entirely, as the second loop demonstrates.
Use a float step only when the exact number of passes does not matter. The float keyword article explains why 0.1 has no exact binary representation.
$ php for_float.php 0.0 -> 0.00000000000000000 0.1 -> 0.10000000000000001 0.2 -> 0.20000000000000001 after loop: 0.3 0.1 0.2 0.3
Nested loops
A loop inside a loop runs completely for every pass of the outer loop. The counter of the inner loop has to be reset before each inner pass.
<?php
declare(strict_types=1);
for ($row = 1; $row <= 3; $row++) {
for ($col = 1; $col <= 4; $col++) {
printf("%4d", $row * $col);
}
echo "\n";
}
for ($row = 1; $row <= 3; $row++) {
for ($col = 1; $col <= 3; $col++) {
if ($row * $col > 4) {
break 2;
}
echo "$row*$col ";
}
}
echo "\n";
The multiplication table uses %4d to align the columns. In the second example the inner loop stops as soon as the product exceeds 4, and break 2 leaves both loops at once; a plain break would only end the inner loop and continue with the next row. The break statement article covers the level argument.
$ php for_nested.php 1 2 3 4 2 4 6 8 3 6 9 12 1*1 1*2 1*3 2*1 2*2
Iterating over arrays
A for loop can walk an indexed array by using the index as the counter. The next article, PHP foreach, covers the loop that is usually clearer for this job.
<?php
declare(strict_types=1);
$names = ["Alice", "Bob", "Carol"];
for ($i = 0; $i < count($names); $i++) {
printf("%d. %s\n", $i + 1, $names[$i]);
}
$total = 0;
$prices = [19.99, 5.50, 12.00];
for ($i = 0, $count = count($prices); $i < $count; $i++) {
$total += $prices[$i];
}
echo "Total: ", number_format($total, 2), "\n";
count($names) is evaluated before every pass. For a large array that does not change inside the loop, compute the count once in the initializer, as the second loop does with $count. The loop assumes that the keys are consecutive integers starting at 0; an associative array or an array with gaps needs foreach or array_values first.
$ php for_array.php 1. Alice 2. Bob 3. Carol Total: 37.49
Best practices
A for loop is easiest to read when its header tells the whole story.
Source
This article covered the PHP for loop: its three clauses, counting in both directions, omitted expressions, loop control, float pitfalls, and nested loops. Continue with PHP foreach for arrays, or read PHP while for condition-driven loops.
Author
List all PHP tutorials.