PHP: For loop

This entry is part 28 of 54 in the series PHP Tutorial

For loops are the most complex loops in PHP.  The syntax of a for loop is:

for (expr1; expr2; expr3)

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed.

If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed).

for ($i = 1; $i <= 10; $i++) {
    echo $i;
    echo "\n";

In this simple example, $i is first initialized to 1.  The condition $i <= 10 is checked and if evaluated to true, the 2 nested statements are executed.

Series Navigation<< PHP: While loop
PHP: Functions >>

Leave a comment

Leave a Reply