Table of Contents



Introduction

The for loop loops through a block of code a specified number of times. It is used when we know in advance how many times the code block should run.

Syntax of the for Loop

The syntax of the for loop is given as:

Syntax

The for loop.

1for (init counter; test counter; increment counter) {
2    statements;
3}

where init counter, test counter, increment counter are all valid PHP expressions such that:

  • init counter: initializes the loop counter value.
  • test counter: evaluated for each loop iteration. If it evaluates to true, the loop continues. If it evaluates to false, the loop ends.
  • increment counter: increases the loop counter value.

Each of the expressions can be empty or contain multiple expressions separated by commas. In test counter, all expressions separated by a comma are evaluated but the result is taken from the last part. An empty test counter means the loop will be run indefinitely (PHP implicitly considers it as perenially true).

There is also an alternative syntax.

Syntax

The for loop (alternative syntax).

1for (init counter; test counter; increment counter):
2    statements;
3endfor;

where the colon : and endfor; replace the braces.

ADVERTISEMENT



Using the for Loop

Example

Using the for loop.

1<?php
2    for ($i = 1; $i <= 5; $i++) {
3        echo "The number is: $i <br>";
4    }
5?>
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

Example

Using the for loop with if and break.

1<?php
2    for ($i = 1; ; $i++) {
3        if ($i > 5) {
4            break;
5        }
6        echo "The number is: $i <br>";
7    }
8?>
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

In the above example, the condition if ($i > 5) break; tells PHP to exit the loop when $i increases beyond 5. Notice that the test counter is empty since it is replaced by the if and break statements.

Example

Using the for loop to sum all even numbers up to 10.

1<?php
2    $sum = 0;
3    for ($i = 0; $i <= 10; $i+=2) {
4        $sum += $i;
5        echo "+ $i = $sum <br>";
6    }
7    echo "The final sum is: $sum <br>";
8?>
+ 0 = 0
+ 2 = 2
+ 4 = 6
+ 6 = 12
+ 8 = 20
+ 10 = 30
The final sum is: 30

Notice the use of an increment counter $i+=2 to include only even numbers.

Example

Using the for loop to generate a 3 times table.

1<?php
2    for ($count = 1 ; $count <= 10 ; ++$count)
3        echo "$count times 3 is " . $count * 3 . "<br>";
4?>
1 times 3 is 3
2 times 3 is 6
3 times 3 is 9
4 times 3 is 12
5 times 3 is 15
6 times 3 is 18
7 times 3 is 21
8 times 3 is 24
9 times 3 is 27
10 times 3 is 30

The outcome of the following example may come as a surprise, especially for those who come from other programming languages like Python or JavaScript.

Example

Counter variable in a for loop.

1<?php
2    for ($i = 1; $i <= 5; $i++) {
3        echo "$i <br>";
4    }
5    echo $i;
6?>
1
2
3
4
5
6

Note that the final value of $i is not 5 as the test counter dictates. It continues to increase by 1 after exiting the for loop.

Caution

The counter variable in a PHP for loop will increment one more time after exiting the loop.

ADVERTISEMENT



Example

Using the for loop with 2 variables.

1<?php
2    for ($i = 1, $j = 1; $i <= 10, $j <= 10; $i++, $j++){
3        echo "$i times $j is " . $i * $j . "<br>";
4
5    }
6?>
1 times 1 is 1
2 times 2 is 4
3 times 3 is 9
4 times 4 is 16
5 times 5 is 25
6 times 6 is 36
7 times 7 is 49
8 times 8 is 64
9 times 9 is 81
10 times 10 is 100

Example

Using the for loop with multiple test expressions.

All expressions separated by a comma are evaluated but the result is taken from the last one.

1<?php
2    for ($i = 1, $j = 1; $i <= 10, $j <= 10; $j += $i){
3        echo "$i times $j is " . $i * $j . "<br>";
4
5    }
6?>
1 times 1 is 1
1 times 2 is 2
1 times 3 is 3
1 times 4 is 4
1 times 5 is 5
1 times 6 is 6
1 times 7 is 7
1 times 8 is 8
1 times 9 is 9
1 times 10 is 10

There are two test expressions $i <= 10 and $j <= 10. However, as long as the last expression $j <= 10 is satisfied, the loop terminates.

Conversely, if we had used $i += $j as the increment counter, the loop will run endlessly since the last expression $j <= 10 will never be satisfied as $j is never incremented.

Example

Using the for loop with multiple test expressions (last condition not satisfied).

1<?php
2    for ($i = 1, $j = 1; $i <= 10, $j <= 10; $i += $j ){
3        echo "$i times $j is " . $i * $j . "<br>";
4
5    }
6?>
1 times 1 is 1
2 times 1 is 2
3 times 1 is 3
4 times 1 is 4
5 times 1 is 5
6 times 1 is 6
7 times 1 is 7
8 times 1 is 8
9 times 1 is 9
10 times 1 is 10
11 times 1 is 11
12 times 1 is 12
13 times 1 is 13
14 times 1 is 14
15 times 1 is 15
...

The loop executes endlessly even though $i has been incremented beyond 10.