for Loop in PHP

Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four types of loop.
  • for loop
  • while loop
  • do...while loop
  • foreach loop

The for loop statement : In PHP for loops execute a block of code a specified number of times.

Syntax :
for ( initialization; condition; increment )
{
statement(s);
}

initialization — it is used to initialize the counter variables.
condition — in the beginning of each iteration, condition is evaluated. If it evaluates to true, the loop continues and the nested statements are executed. If it evaluates to false, the execution of the loop ends.
increment — it updates the loop counter with a new value. It is evaluate at the end of each iteration.

Example :
<?php
for( $i = 1; $i <=10; $i++)
{
echo $i."<br/>";
}
?>

Output :
1
2
3
4
5
6
7
8
9
10

0 Comments