Topic outline

• PHP - Loop Types

• Lesson

WHILE LOOP IN PHP

While loop, loops through a block of code as long as the specified condition is true. The simplest kind of loop in PHP is the while loop.

Like an if statement, it relies on a condition. The difference between a while loop and an if statement is that an if statement executes the code that follows it only once if the condition is true.

A while loop executes the block repeatedly for as long as the condition is true.

SYNTAX:

While (condition)

{

code to be executed;

}

WHILE LOOP EXAMPLE

<html>

<body>

<?php

$i=1; /* here <condition> is a Boolean expression. Loop body is executed as long as condition is true*/ while($i<5)

{

echo "i is = $i <br>";$i++;

}

?>

</body>

</html>

OUTPUT of the above given Example is as follows:

i is = 1  i is = 2  i is = 3  i is = 4

FOR AND FOREACH LOOPS

PHP for loop executes a block of code, a specified number of times

SYNTAX:

For(expression1;condition;expression2)

expression3;

expression1 is executed once at the start. Here, you usually set the initial value of a counter.

The condition expression is tested before each iteration. If the expression returns false, iteration stops. Here, you usually test the counter against a limit.

expression2 is executed at the end of each iteration. Here, you usually adjust the value of the counter.

expression3 is executed once per iteration. This expression is usually a block of code and contains the bulk of the loop code.

The following program illustrates the example for loop:

<html>

<body>

<?php

echo "Numbers from 1 to 20 are: <br>";

/*in for loop, initialization usually declares a loop variable, condition is a Boolean expression such that if the condition is true, loop body will be executed and after each iteration of loop body, expression is executed which usually increase or decrease loop variable*/

for ($x=0;$x<=20; $x++) { echo "$x  ";

}

?>

</body>

</html>

OUTPUT of the above given Example is as follows:

Numbers from 1 to 20 are:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

DECLARING MULTIPLE VARIABLES IN FOR LOOP EXAMPLE

<html>

<body>

<?php

/* Multiple variables can be declared in   declaration block of for loop */

for ($x=0,$y=1,$z=2;$x<=3;$x++) { echo "x =$x,  y = $y, z =$z <br>";

}

?>

</body>

</html>

OUTPUT of the above given Example is as follows:

x = 0, y = 1, z = 2  x = 1, y = 1, z = 2  x = 2, y = 1, z = 2  x = 3, y = 1, z = 2

DO WHILE LOOP IN PHP

Do while loop will always execute the block of code once, it will then check the condition, and if the condition is true then it repeats the loop

SYNTAX:

do

code to be executed;

} while (condition );

DO WHILE LOOP EXAMPLE

The final loop type we describe behaves slightly differently. The general structure of a do...while statement is

do

expression;

while(condition);

A do...while loop differs from a while loop because the condition is tested at the end. This means that in a do...while loop, the statement or block within the loop is always executed at least once.

Even if you consider this example in which the condition will be false at the start and can never become true, the loop will be executed once before checking the condition and ending:

<html>

<body>

<?php

$i=1; /* here <condition> is a Boolean expression. Please note that the condition is evaluated after executing the loop body. So loop will be executed at least once even if the condition is false*/ do { echo "i is =$i <br>";

$i++; }while($i<5);

?>

</body>

</html>

OUTPUT of the above given Example is as follows:

i is = 1  i is = 2  i is = 3  i is = 4