LOOP

FOR LOOP SYNTAX :

for (init counter; test counter; increment counter)

{

code to be executed;

}

<?php

for($i=0; $i<5;$i++)

{

echo $i;

}

?>

For loop Example :

<?php

$a=5;

for($i=1;$i<=10;$i++)

{

echo ” 5*”.$i.”==”.$a*$i.'<br>’;

}

?>

Output:

5*1==5
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50

WHILE LOOP SYNTAX:

The while loop executes a block of code as long as the specified condition is true.

While (condition is true)

{

code to be executed;

}

EXAMPLE :

<?php

$i=0;

while($i<5)

{

echo $i;

$i++;

}

?>

While Loop Example :

<?php

$a=5;

$i=1;

while($i<=10)

{

echo ” 5*”.$i.”==”.$a*$i.'<br>’;

$i++;

}

?>

output :

5*1==5
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50

The PHP do…while Loop

The do…while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

do

{

code to be executed;

}

while (condition is true);

EXAMPLE :

<?php

$i=0;

do

{

echo $i;

$i++;

}

while($i<5);

?>

Do-while Loop Example :

<?php

$a=5;

$i=1;

do

{

echo ” 5*”.$i.”==”.$a*$i.'<br>’;

$i++;

}

while($i<=10);

?>

Output :

5*1==5
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50

Simple Application for Practice use of Loop:

(1)write code for table.html file:

<form action=”table.php” method=”post”>

enter your number<input type=”text” name=”number”>

<input type=submit name=submit value=submit>

</form>

(2)write code for table.php file:

<?php

$number=$_POST[‘number’];

for($i=1;$i<10;$i++)

{

echo $number*$i.'<br>’;

}

?>
output:

after click on submit button you will get result.