Looping Control Statement in PHP
|
Sometimes
it is required to perform same task again and again repeatadely in the script.
In such situations Looping Control Statements are used in PHP. |
||
While Loop |
||
|
Syntax
while (condition) { body of loop; } While
loop first check the condition. If condition is true then the statements written
inside body of loop are executed. After executing statements inside body of loop
again the condition is checked.
Example <?php $i=1; while($i<=10) { echo $i." "; $i++; } ?> Output 1 2 3 4 5 6 7 8 9 10 |
Do While Loop
do
{
body of loop;
} while (condition);
In
do … while loop first statements written inside body of the loop are executed. After
executing statements inside body of the loop the condition is checked. If
condition is true then the statements written inside body of loop are executed again.
This
process is repeated until condition specified within do … while loop becomes false.
As the condition becomes false the statements inside body of loop is not executed
and the statements followed by the do … while loop is executed.
It
is also known as exit controlled loop because the condition is checked at last and
then body of the loop is executed based on the condition. So even if condition is
false at the first trial the body of loop executes at least once.
<?php
$i=1;
do
{
echo $i." ";
$i++;
}while($i<=10);
Output
1 2 3 4 5 6 7 8 9 10
For Loop
for(initialization;condition;iteration)
{
body of loop;
}
Here,
Initialization: It indicates the starting point for the loop. Starting point specifies from which value the loop will start the iteration.
Condition:It indicates control condition for the loop. Control condition determines when loop will stop.
Iteration:It indicates how the value of the counter variable that holds the starting value will vary. You can either increment or decrement the value of the counter variable based on your requirement.
for
loop is used to repeatedly perform some task for specific number of times. It is
also known as iterative statement. It is useful when you know in advance how many
times you want to repeat the task.
In for loop first the counter variable is initialized with the starting value in
the initialization part. After initialization the condition is checked. If the condition
is true then the statements written inside body of loop will executes. After executing
statements inside body of loop the control goes to the Iteration part where the
value of the counter variable is incremented or decremented. After changing the
value of the counter variable it again check the condition.
This
process is repeated until condition specified within for loop becomes false. As
the condition becomes false the statements inside body of loop will not executes
and control transfer immediately to the statement followed by for loop.
<?php
for ($i=1;$i<=10;$i++)
{
echo $i. " ";
}
?>
Output
1 2 3 4 5 6 7 8 9 10
foreach loop
foreach ($ArrayName as $Value)
{
Body of loop
}
foreach
looping structure is used to iterate throgh each elements of an array starting from
first element.
In
foreach loop how many times the loop will iterate depands on the number of elements
in an array.
foreach
looping structure is used specially with Associative array.
<?php
$MyArray = array (Yesha, Vidhi, Jiwansh);
foreach ($MyArray as $Value)
{
echo $value. " ";
}
?>
Output
Yesha Vidhi Jiwansh