PHP Looping with the "for" Statement
Learn the PHP "for" loop: initialization, condition, and increment — plus counting up/down, custom steps, array iteration, nested loops, break, and continue.
In PHP, the for statement is a control structure that runs a block of code a known number of times. You reach for it whenever the iteration count is fixed and predictable — printing the numbers from 1 to 10, building an HTML table with N rows, or stepping through an indexed array by position.
This chapter covers the three parts of a for loop, how each one runs, and practical patterns: counting up and down, custom step sizes, looping over arrays, nesting loops, and the break / continue keywords.
Syntax
A for loop is built from three expressions inside the parentheses, separated by semicolons, followed by the loop body in braces:
for (initialization; condition; increment) {
// code to be executed on each pass
}The three expressions run in a specific order:
- Initialization runs once, before the loop starts — it usually sets up a counter.
- Condition is checked before every iteration. While it is
true, the body runs; when it becomesfalse, the loop stops. - Increment runs after every iteration, just before the condition is checked again — it usually advances the counter.
Initialization
The initialization sets the counter's starting value. It executes a single time at the very beginning. To start counting at 1:
$counter = 1;Condition
The condition decides when to stop. The loop keeps running as long as the condition is true. To loop while the counter is 10 or less:
$counter <= 10;If the condition is false on the first check, the body never runs at all.
Increment
The increment changes the counter after each pass so the loop eventually ends. To add 1 each time:
$counter++;If the increment never makes the condition false, you get an infinite loop. Make sure the counter moves toward the stop condition.
A complete counting example
Putting the three parts together, this loop prints the value of $counter on each pass, from 1 through 10:
The above code outputs:
1 2 3 4 5 6 7 8 9 10 (Without the . " ", echo would print the digits with no gaps — 12345678910 — because echo adds nothing between values.)
Counting down and custom steps
The increment is just an expression, so it can decrement or move in steps other than 1. To count down from 10 to 1, start high, stop at 1, and use $i--:
<?php
for ($i = 10; $i >= 1; $i--) {
echo $i . " ";
}
?>To step by 2 (printing even numbers up to 10), use $i += 2:
<?php
for ($i = 2; $i <= 10; $i += 2) {
echo $i . " "; // 2 4 6 8 10
}
?>Looping through an array
A for loop can walk an indexed array by position. The condition uses count() to know how many elements there are, and $i is used as the array index:
The above code outputs:
apple
banana
cherryNote the loop starts at 0 (PHP arrays are zero-indexed) and the condition is $i < count($fruits) — using < rather than <= because the last valid index is count - 1.
For arrays — especially associative arrays where keys are not sequential integers — the foreach loop is usually clearer and safer than a for loop, since you don't manage an index by hand.
Nested loops
You can place one for loop inside another. The inner loop runs completely for every single pass of the outer loop. This is common for grids, tables, and multiplication-style output:
<?php
for ($row = 1; $row <= 3; $row++) {
for ($col = 1; $col <= 3; $col++) {
echo $row * $col . "\t";
}
echo "\n";
}
?>This prints a 3×3 multiplication grid:
1 2 3
2 4 6
3 6 9 Breaking out and skipping iterations
Two keywords give you finer control inside the loop body:
breakstops the loop immediately and continues with the code after it.continueskips the rest of the current iteration and jumps to the next one (the increment still runs).
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 4) {
continue; // skip 4
}
if ($i == 7) {
break; // stop entirely at 7
}
echo $i . " ";
}
?>The above code outputs:
1 2 3 5 6 4 is skipped by continue, and the loop stops before printing 7 because of break. Learn more in the break and continue chapter.
When to use for vs other loops
- Use
forwhen you know the number of iterations up front, or need a counter / index. - Use
foreachto iterate over the elements of an array or object without managing an index. - Use
whilewhen you loop until some condition changes and there is no natural counter. - Use
do...whilewhen the body must run at least once before the condition is checked.
Conclusion
The for loop is an essential control structure in PHP for repeating code a known number of times. Master its three parts — initialization, condition, and increment — and you can count up or down, step by any amount, walk arrays by index, and nest loops to build grids. For element-by-element array iteration, prefer foreach; for indefinite repetition, reach for while.