Menu

PHP for Loop: Syntax, Arrays, break and continue

for ($i = 0; $i < 10; $i++) { ... } runs a block a set number of times: start value, condition checked before each pass, and a step. Covers looping through an array by index, counting down, nested loops, break and continue.

This page includes runnable editors - edit, run, and see output instantly.

for ($i = 0; $i < 5; $i++) { ... } runs the block a fixed number of times. The three parts are the start ($i = 0, runs once), the condition ($i < 5, checked before every pass) and the step ($i++, runs after every pass). The loop ends as soon as the condition is false.

$i still exists after the loop and holds the value that made the condition false (6). PHP has no block scope for loop variables.

Loop through an array with an index

For a list with keys 0, 1, 2 ..., the counter doubles as the index. Store count() in a variable first so it is not called again on every pass.

The first part can hold several expressions separated by commas, which is how $n = count($fruits) gets set once. When you only need each value (and not its neighbours), foreach is shorter and also works on arrays whose keys are not 0 to n - 1.

Count down and step by more than 1

The step can be any expression: $i-- to count down, $i += 2 for every second number, $i *= 2 for powers of two.

range('a', 'h') builds the array ['a', 'b', ... 'h']. With range(1, 10) and foreach you get the same counting as a for loop, at the cost of building the whole array first.

break and continue

break ends the loop right away. continue skips the rest of the current pass and jumps to the step and the next condition check.

The second loop has an empty condition, which means "always true", so it runs until break. In nested loops, break 2 and continue 2 act on the outer loop, as the next example shows.

Nested for loops: a multiplication table

A loop inside a loop runs the inner loop completely for every pass of the outer one. Here the output is HTML, so the Page tab shows a real table.

Change $size to 12 and run it again. The for (...): and endfor; form is the alternative syntax that keeps loops readable between HTML tags. The same table built as plain text, and with break 2 to stop both loops:

Common mistakes: off by one and the endless loop

The two classic for bugs: using <= with count() reads one element past the end, which PHP reports as Warning: Undefined array key 3 and treats as null; and a step that never moves the counter toward the end ($i-- where you meant $i++) keeps the condition true forever.

For loops whose length you cannot know in advance, such as reading until a file ends, a while loop fits better.

Frequently Asked Questions

How do I loop through an array with an index in PHP?

For a list with keys 0, 1, 2 use for ($i = 0; $i < count($arr); $i++) { echo $arr[$i]; }. In most code foreach ($arr as $i => $value) is simpler, gives the same index, and also works when the keys are not 0 to n minus 1.

How do I break out of a for loop in PHP?

break; leaves the loop immediately. Inside nested loops, break 2; leaves the inner loop and the one around it. continue; skips the rest of the current pass and moves on to the next one.

How do I count down with a for loop in PHP?

Start high, compare with >= and decrement: for ($i = 10; $i >= 1; $i--) { echo $i; }. For steps other than one, change the third part, for example $i += 2 or $i -= 5.

When should I use for instead of foreach in PHP?

Use for when you count something (repeat 5 times, numbers from 1 to 100, every second element) or need the next or previous element by index. Use foreach to visit every element of an array, especially associative arrays, where there is no numeric index.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED