Skip to main content

The PHP for Loop

Reach for a for loop when you know in advance how many times the code has to run. All the details that control the repetition sit together in one line at the top of the loop.

That line has three parts:

  • initialization - runs one time, before the loop starts, and usually sets up a counter variable
  • condition - is tested before every iteration; the loop keeps going while it is true
  • increment - runs after every iteration and usually raises or lowers the counter

Print numbers from 0 to 10

This loop counts from 0 up to and including 10, printing each number:

Prompt
Write a PHP script that uses a for loop to print the numbers from 0 to 10. In each iteration, echo "The number is: " followed by the current number and a line break.
Output: PHP script

For loop syntax

Written out in general form, a for loop looks like this:

index.php
for (initialization; condition; increment) {
  // code to be executed
}

Example Explained

Let's walk through that example. First, $x = 0; runs a single time and gives the counter its starting value. Before every pass, PHP checks $x <= 10;, and the loop goes on only if the result is true, which is the case while $x has not gone past 10. After every pass, $x++; adds one to $x.


Decrease Counter Variable

Counters don't have to go up. Start high and subtract on each pass, and the loop counts backwards, as the next example shows.

Print numbers from 10 to 0

Starting at 10, $x-- lowers the counter until it reaches 0:

Prompt
Write a PHP script that uses a for loop to print the numbers from 10 to 0. Start at 10, decrease by 1 each iteration, and echo "The number is: " followed by the current number and a line break.
Output: PHP script

The PHP break Statement

A for loop can be ended early with break, even while its condition is still true.

Stop the loop with break

The loop is set up to reach 10, but it ends when $x equals 3, so only 0, 1 and 2 are printed:

Prompt
Write a PHP script that uses a for loop from 0 to 10. If the counter equals 3, use break to stop the loop. Otherwise echo "The number is: " followed by the counter and a line break.
Output: PHP script

The PHP continue Statement

If you only want to skip one pass rather than end the loop, use continue. PHP abandons that iteration and moves on to the following one.

Skip iteration with continue

Every number from 0 to 10 is printed except 3:

Prompt
Write a PHP script that uses a for loop from 0 to 10. If the counter equals 3, use continue to skip that iteration. Otherwise echo "The number is: " followed by the counter and a line break.
Output: PHP script

Step 10

The increment doesn't have to be 1. Adding 10 on every pass gives you a loop that counts in tens.

Count to 100 by tens

Using $x+=10 as the increment, this for loop goes from 0 to 100 in steps of ten:

Prompt
Write a PHP script that uses a for loop to count from 0 to 100 by tens. In each iteration, echo "The number is: " followed by the current number and a line break.
Output: PHP script