PHP LOOP
When you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task.
When you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task.
OR
When you want to
execute same statements more than one times then use loop.
Four
types of Loop used in PHP :
for : In for loop
specified condition is predefined.
while : while loop
executes the statement while a specified condition is true.In while loop first
check the condition then execute the statement.
do-while : do – while
loop executes the statement once and next statement depends upon a specified
condition is true. In do – while loop first execute the statement then check
the condition, it means if condition is false in that case one time statement
execute.
foreach – foreach loop is used to
print associative array’s value.
for loop in PHP
The for loop is used
when you know in advance how many times the script should run.
Syntax
for (initialization; condition; increment)
{
code to be
executed;
}
Parameters
of for loop :
Initialization
:is used
to set a counter.
Condition
: if
condition is true loop will continue, if the condition is false loop ends.
Increment : used to increment the counter.
print the statement 5 times
<?php
for ($i=1;
$i<=5; $i++)
{
echo "The Number is: ".$i."<br/>";
}
?>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Find the sum of 1 to 100.
<?php
$sum=0;
for ($i=1; $i<=100; $i++)
{
$sum=$sum+$i;
}
echo $sum;
?>
Output : 5050
while loop in php
The while loop executes a
block of code while a condition is true.
Syntax
while (condition)
{
code to be
executed;
}
Example:-
<?php
$i=1;
while($i<=5)
{
echo "The
number is " . $i . "<br>"; $i++;
}
?>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The
number is 5
do while loop in PHP
The do…while Loop executes
the statement once and then check the condition.
Syntax:-
do
{
code to be executed;
}
while (condition);
<?php $i=1;
do
{
echo "The number is " . $i . "<br>";
$i++;
}
while ($i<=5);
?>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
foreach loop in PHP
The foreach Loop is used to
display the value of array.
You can define two
parameter inside foreach separated through
“as” keyword.
First parameter must be existing array name which elements or key you want to be used.
First parameter must be existing array name which elements or key you want to be used.
at the position of
2nd parameter You can define two variables
One for key(index)
another for value
another for value
if you define only
one variable at the position on 2nd parameter it contain arrays value.
Syntax
Syntax
foreach ($array as $value) { code to be executed; }
print the values of the given array.
<?php
$person=array("alex", "simon","ravi");
foreach ($person as $val)
{
echo
$val."<br/>";
}
?>
Output : alex simon ravi