Loop means do repeat same work.Loop is used to do any calculation or work again and again until the condition say to stop.There are four kind of loop which are given below-
1) for loop
2) Do while loop
3) While loop.
4) foreach loop
The for loop statement
The for statement is used when you know how many times you want to execute a statement or a block of statements.
Syntax of for.....statement
<?php
for (initialization; condition; increment/decrement)
{
execute if codition is true;
}
?>
initialization we use starting point. From which position we will start and condition indicate how many time or until which position it will execute. Increment or decrement work after execution if needed according to condition.
Example
<?php
for($i=1;$i<=10;$i++)
{
echo $i;
}
?>
According to above example value of $i i.e. 1 is execute if it is with in 10 number according to condition than it will execute. Then it will increment 1 or add 1 and check again after that execute it.
So It will work like this Increment--check--execute again and again until the condition say stop.
For ,Do while and While loop are similar at the site of their activities. Same block of statements can do by any of them.Example is given below according to for loop.
Syntax Do while loop
<?php
Do
{
execute if codition is true;
}
while (condition)
?>
Example
<?php
$i=1;
Do
{
$i++; echo $i;
}
while($i<=10;)
?>
Syntex of the while loop
<?php
while (condition)
{
execute if codition is true;
}
?>
Example
<?php
while($i<=10;)
$i=1;
{
$i++; echo $i;
}
?>