Logo


PHP Loop For



Loop For

Loop is used to execute the same set of statements with a specified number of times(until a condition is satisfied) in incremental or detrimental way.
For loop executes a certain condition as many times as you want.
All the three parameters are separated by semicolon ";".

Syntax
for(intial value; condition; increment)
{
// set of statements that will be executed
}

Example: 1

<?php
for($num=1$num<=5$num++)
{
echo 
$num."<br />";
}
?>
Output
1
2
3
4
5

Note: When “i<=5” executes, then condition remain true and loop terminates when “i=6”. “i<=5” says “i is less than or equal to 5”.


Example: 2

<?php
$sum
=0;
for(
$i=1$i<5$i++)
{
$sum $sum+$i;
}
echo 
"The sum of 1 through 4 is: ".$sum;
?>
Output
The sum of 1 through 4 is: 10

Example: 3
<?php
$Month 
= array("January","February ","March","April","May");
for(
$i 0$i count($Month); $i++)
{
echo 
$Month[$i]."<br />";
}
?>
Output
January
February
March
April
May



If you like this Article, Share and Enjoy



See Also
Foreach Loop
While Loop
Do While Loop