W3docs

PHP Looping Techniques: A Comprehensive Guide

Learn PHP loops: for, while, do-while, and foreach with runnable examples, plus break and continue, nested loops, and tips on choosing the right loop.

Loops let you run the same block of code over and over without copying and pasting it. Instead of writing ten echo statements, you write one and tell PHP to repeat it ten times. Almost every real program loops over something: rows from a database, items in a shopping cart, lines in a file, or numbers in a range.

PHP gives you four loop constructs:

  • for — when you know how many times to repeat, or you need a counter.
  • while — repeat as long as a condition stays true (you may not know how many times in advance).
  • do...while — same as while, but the body always runs at least once.
  • foreach — the go-to loop for walking through every element of an array.

This page covers the syntax of each, how to control loops with break and continue, nested loops, and how to pick the right one.

For loops

A for loop repeats a block a known number of times. Its header has three parts separated by semicolons:

for (initializer; condition; update) {
  // body
}
  1. Initializer runs once, before the loop starts — usually to set up a counter.
  2. Condition is checked before every iteration. The loop runs while it is true and stops the moment it becomes false.
  3. Update runs after each iteration — usually to increment the counter.
php— editable, runs on the server

Here $i starts at 0, the body runs while $i < 10, and $i++ adds 1 after each pass. The loop prints The number is 0 through The number is 9 — ten lines. Note it stops at 9, not 10, because the condition $i < 10 is already false when $i reaches 10. The relational operators used here (<, ++) are covered in PHP operators.

While loops

A while loop repeats as long as its condition is true. Use it when the number of iterations isn't fixed in advance — for example, reading until you reach the end of a file.

php— editable, runs on the server

This prints the same ten lines as the for example above. The difference is purely stylistic: with while you manage the counter yourself ($i = 0 before the loop, $i++ inside it). Forgetting the $i++ would make the condition stay true forever — an infinite loop, the most common loop bug.

Do-while loops

A do...while loop is like while, but it checks the condition after running the body. That means the body always executes at least once, even if the condition is false from the start.

php— editable, runs on the server

This also prints 0 through 9. The "at least once" behaviour matters when the condition depends on something the loop body produces — for instance, prompting a user for input and repeating only if the input was invalid. Notice the semicolon after while (...); it is required here.

Foreach loops

A foreach loop walks through every element of an array, one at a time. It's the cleanest way to iterate over a collection because you never manage an index by hand.

The simplest form gives you each value:

php— editable, runs on the server

On each pass, $value holds the next element, so this prints Red, Green, then Blue.

For associative arrays, use the $key => $value form to read both the key and the value:

php— editable, runs on the server

This prints Peter is 32, Quagmire is 30, and Joe is 34. foreach preserves insertion order, so the elements come out in the order you defined them.

Loop Control: break and continue

Sometimes you need to stop a loop early or skip a single pass. That's what break and continue are for, and they work in every loop type.

PHP break statement

The break statement stops the loop immediately and jumps to the code after it.

php— editable, runs on the server

When $i reaches 5, break ends the loop, so this prints only Number: 0 through Number: 4.

PHP continue statement

The continue statement skips the rest of the current iteration and jumps straight to the next one — the loop itself keeps going.

php— editable, runs on the server

Here continue skips the iteration where $i == 5, so every number except 5 is printed: Number: 0 to Number: 4, then Number: 6 to Number: 9.

Nested loops

You can place one loop inside another. The inner loop runs completely on every single pass of the outer loop. This is how you build grids, tables, and multiplication charts.

<?php

for ($row = 1; $row <= 3; $row++) {
  for ($col = 1; $col <= 3; $col++) {
    echo $row * $col . "\t";
  }
  echo "\n";
}

?>

The outer loop runs three times (one per row); for each row the inner loop runs three times (one per column), printing a 3×3 multiplication grid. Note that inside a nested loop, break and continue affect only the loop they sit in. To break out of more than one level at once, you can pass a number: break 2; exits two loops.

Which loop should I use?

SituationBest choice
You know the iteration count or need a counterfor
Repeat while a condition holds, count unknownwhile
Like while, but the body must run at least oncedo...while
Process every element of an arrayforeach

When in doubt for arrays, reach for foreach — it is the most readable and the hardest to get wrong, since there's no index to manage and no risk of going out of bounds.

Conclusion

PHP's four loops cover every repetition pattern you'll meet: for for counted loops, while and do...while for condition-driven loops, and foreach for arrays. Combine them with break and continue to control the flow, and nest them to work with grids and tables. Once loops feel natural, pair them with conditional statements, the switch statement, and functions to structure full programs.

Practice

Practice
Which of the following are valid loop types in PHP?
Which of the following are valid loop types in PHP?
Was this page helpful?