PHP Tutorial Tutorials - PHP for Loop

PHP for Loop

The syntax of a forloop is:

for (expr1; expr2; expr3)
    statement

We can group several statements into a statement group

for (expr1; expr2; expr3){
    statement1;
    statement2;
}
  • expr1 would be executed once at the beginning of the loop.
  • expr2 is conditional expression. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. Otherwise the execution of the loop ends.
  • expr3 would be executed at the end of each iteration.
<?php
for($i=1; $i<3; $i++){
    echo "i = $i <br>";
}
var_dump($i);
?>

Output:

i = 1 
i = 2 
int(3)

Alternative syntax for For loop statement

PHP also supports the alternate "colon syntax" for for loops.

<?php
for($i=1; $i<3; $i++):
    echo "i = $i <br>";
endfor;
?>
Date:2019-10-01 02:36:58 From:www.Lautturi.com author:Lautturi