PHP loop

PHP Loop@ Shwetank education.com

Script   For Loop in php
A for loop is used when we want to execute a block of code for certain number of times

Syntax

for(initialize counter; condition until counter is reached; increment counter)
{
//execute block of code
}

Example

<?php
for($i=0; $i<=50; $i=$i+1)
{
echo $i;
}
?>

Output : Try yourself!

Script   Nested For Loop in php

<?php
$a;
$b;
{
for($a=1;$a<=10;$a++)
{
for($b=1;$b<=$a;$b++)
{
print “*”;
}
print “
“;
}
}
?>

Output:
Try IT

Script   For-each Loop in php

<?php
$browsers = array (“Jan”, “feb”, “marc”);
echo “<select>”;
foreach($browsers as $browser)
{
echo “<option name=’$browser’>$browser</option>”;
}
echo “</select>”;
?>

Output: Try it

Script   While Loop in php

<?php

$i = 1;
while ($i <= 5 )
{
echo $i . “
“;

$i = $i + 1;
}
?>

Output:

Try IT