There are four types of loops in PHP. They are as follows:
1) while loop
2) do…while loop
3) for loop
4) foreach loop
1) while loop:
Loops through a block of codes while a specified condition is true.
Example:
<?php
$i=1;
while($i<=5)
{
echo “The number is ”.$i.”<br/>”;
$i++;
}
?>
2) do…while loop:
Loops through a block of code once and then repeats the loop as long as a specified condition is true.
Example:
<?php
$i=1;
do{
$i++;
echo “The number is ”.$i.”<br/>”;
} while($i<=5);
?>
3)for loop:
Loops through a block of code a specified number of items.
Example:
<?php
for($i=1;$i<=5;$i++)
{
echo “The number is ”.$i.”<br/>”;
}
?>
4) foreach loop:
Loops through a block of code for each element in an array.
Example:
<?php
$x=array(“one”,”two”,”three”);
foreach($x as $value)
{
echo $x.”<br/>”;
}
?>
1) while loop
2) do…while loop
3) for loop
4) foreach loop
1) while loop:
Loops through a block of codes while a specified condition is true.
Example:
<?php
$i=1;
while($i<=5)
{
echo “The number is ”.$i.”<br/>”;
$i++;
}
?>
2) do…while loop:
Loops through a block of code once and then repeats the loop as long as a specified condition is true.
Example:
<?php
$i=1;
do{
$i++;
echo “The number is ”.$i.”<br/>”;
} while($i<=5);
?>
3)for loop:
Loops through a block of code a specified number of items.
Example:
<?php
for($i=1;$i<=5;$i++)
{
echo “The number is ”.$i.”<br/>”;
}
?>
4) foreach loop:
Loops through a block of code for each element in an array.
Example:
<?php
$x=array(“one”,”two”,”three”);
foreach($x as $value)
{
echo $x.”<br/>”;
}
?>
Comments
Post a Comment