Looping In PHP

The for loop statement

The for statement is used when you know how many times you want to execute a statement or a block of statements.

Syntax

for (initialization; condition; increment){
code to be executed;
}

The initializer is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i.

Example

The following example makes five iterations and changes the assigned value of two variables on each pass of the loop −

<html>
<body>

<?php
$a = 0;
$b = 0;

for( $i = 0; $i<5; $i++ ) {
$a += 10;
$b += 5;
}

echo ("At the end of the loop a = $a and b = $b" );
?>

</body>
</html>

This will produce the following result −

At the end of the loop a = 50 and b = 25

The while loop statement

The while statement will execute a block of code if and as long as a test expression is true.

If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.

Syntax

while (condition) {
code to be executed;
}

Example

This example decrements a variable value on each iteration of the loop and the counter increments until it reaches 10 when the evaluation is false and the loop ends.

<html>
 <body>
 
 <?php
 $i = 0;
 $num = 50;
 
 while( $i < 10) {
 $num--;
 $i++;
 }
 
 echo ("Loop stopped at i = $i and num = $num" );
 ?>
 
 </body>
</html>

This will produce the following result −

Loop stopped at i = 10 and num = 40

The do…while loop statement

The do…while statement will execute a block of code at least once — it then will repeat the loop as long as a condition is true.

Syntax

do {
code to be executed;
}
while (condition);

Example

The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 10 −

<html>
<body>

<?php
$i = 0;
$num = 0;

do {
$i++;
}

while( $i < 10 );
echo ("Loop stopped at i = $i" );
?>

</body>
</html>

This will produce the following result −

Loop stopped at i = 10
Nested Loops

NESTED WHILE LOOP IN PHP :

While loops execute the statement repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the statement, execution will not stop until the end of the iteration. Sometimes, if the while expression evaluates to FALSE from the very beginning, the statement won’t even be run once.

Syntax :

while(condition)

{

while(condition)

{

statement(s);

increment/decrements;

}

statement(s);

increment/decrements;

}

Example:

<?php

int $i = 1 , $j = 1;

while( $i < 3 )

{

while( $j < 3 )

{

echo ‘i am programmer ‘;

$j++;

}

echo ‘<br />’;

$i++;

}

?>

Output:
i am programmer i am programmer
i am programmer i am programmer

NESTED DO WHILE LOOP IN PHP :

do while loops are very similar to while loops, except the truth expression, is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run whereas it may not necessarily run with a regular while loop.

Syntax:

do

{

statement(s);

increment/decrements;

do

{

statement(s); increment/decrements;

}

while( condition );

}

while( condition );

Example:

<?php

int $i=1 , $j=2;

do

{

do

{

echo ‘i am programmer ‘;

$j++;

}

while( $j < 3 )

echo ‘<br />’;

$i++;

}

while( $i < 3 )

?>

Output:
i am programmer i am programmer
i am programmer i am programmer

NESTED FOR LOOP IN PHP :

Syntax:

for ( initialization; condition; increment/decrements )

{

for ( initialization; condition; increment/decrements )

{

statement(s);

}

statement(s);

}

Example:

<?php

int $i , $j;

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

{

for( $j=1; $j<3; $j++)

{

echo ‘i am programmer ‘;

}

echo ‘<br />’;

}

?>

Output:
i am programmer i am programmer
i am programmer i am programmer

NESTED FOREACH LOOP IN PHP :

Syntax:

foreach(array as value)

{

foreach(array as value)

{

Statement;

}

Statement;

}

Example:

<?php

$color= array( “Red”, “Green”, “Yellow”, );

$fruit = array( “Banana”, “Apple”, “Plum”, );

foreach ($color as $c)

{

foreach ($fruit as $f)

{

echo “this is A $c $f .<br/>\n”;

}

}

?>

Output:
this is A Red Banana.
this is A Red Apple.
this is A Red Plum.
this is A Green Banana.
this is A Green Apple.
this is A Green Plum.
this is A Yellow Banana.
this is A Yellow Apple.
this is A Yellow Plum.