Example:
for ($i=1; $i<=10; $i++)
{
echo "Hello World
";
}
?>
Foreach Loop:
Foreach loops allow you to quickly traverse through an array.
$array = array("name" => "Jet", "occupation" => "Bounty Hunter" );
foreach ($array as $val) {
echo "
$val";
}
Prints out:
Jet
Bounty Hunter
You can also use foreach loops to get the key of the values in the array:
foreach ($array as $key => $val) {
echo "
$key : $val";
}
Prints out:
name : Jet
occupation : Bounty Hunter
While Loop:
While loops are another useful construct to loop through data. While loops continue to loop until the expression in the while loop evaluates to false. A common use of the while loop is to return the rows in an array from a result set. The while loop continues to execute until all of the rows have been returned from the result set.
Syntax:
while (condition){
statement(s);
}
Example:
$i=1;
while($i<=9)
{
echo "The number is " . $i . "
";
$i++;
}
?>
Do-While Loop:%