PHP: Looping Statements

Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types.

  • The While Loop
  • The Do...While Loop
  • The For Loop
  • The Foreach Loop
  • Break and Continue Statements

The While Loop

The While statement executes a block of code if and as long as a specified condition evaluates to true. If the condition becomes false, the statements within the loop stop executing and control passes to the statement following the loop. The While loop syntax is as follows:

while (condition) 
{
  code to be executed;
}

Example:

<?php
$i=0;
while ($i <= 10) { // Output values from 0 to 10
   echo "The number is ".$i."<br />";
   $i++;
}
?>

The Do...While Loop

The Do...While statements are similar to While statements, except that the condition is tested at the end of each iteration, rather than at the beginning. This means that the Do...While loop is guaranteed to run at least once. The Do...While loop syntax is as follows:

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

Example:

<?php 
$i = 0;
do {
   echo "The number is ".$i."<br/>";
   $i++;
} 
while ($i <= 10);
?>;

The For Loop

The For statement loop is used when you know how many times you want to execute a statement or a list of statements. . The syntax of For loops is a  little bit more complex, though for loops are often more convenient than While loops. The For loop syntax is as follows:

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

Example:

<?php
for ($i=0; $i <= 10; $i++) 
{
   echo "The number is ".$i."<br />";
}
?>

The Foreach Loop

The Foreach loop is a variation of the For loop and allows you to iterate over elements in an array. There are two different versions of the Foreach loop. The Foreach loop syntaxes are as follows:

foreach (array as value)
{
   code to be executed;
}
    
foreach (array as key => value)
{
   code to be executed;
}

Example:

The example below demonstrates the Foreach loop that will print the values of the given array:

<?php
$email = array('john.smith@example.com', 'alex@example.com');
foreach ($email as $value) {
   echo "Processing ".$value."<br />";
}
?>

An alternative form of Foreach loop gives you access to the current key:

<?php
$person = array('name' => 'Andrew', 'age' => 21, 'address' => '77, Lincoln st.');
foreach ($person as $key => $value) {
   echo $key." is ".$value."<br />";
}
?>

Break and Continue Statements

Sometimes you may want to let the loops start without any condition, and allow the statements inside the brackets to decide when to exit the loop. There are two special statements that can be used inside loops: Break and Continue.

The Break statement terminates the current While or For loop and continues executing the code that follows after the loop (if any). Optionally, you can put a number after the Break keyword indicating how many levels of loop structures to break out of. In this way, a statement buried deep in nested loops can break out of the outermost loop.

Example:

<?php
echo "<p><b>Example of using the Break statement:</b></p>";

for ($i=0; $i<=10; $i++) { 
   if ($i==3){break;} 
   echo "The number is ".$i;
   echo "<br />"; 
}

echo "<p><b>One more example of using the Break statement:</b><p>";

$i = 0;
$j = 0;

while ($i < 10) {
  while ($j < 10) {
    if ($j == 5) {break 2;} // breaks out of two while loops
    $j++;
  }
  $i++;
}

echo "The first number is ".$i."<br />";
echo "The second number is ".$j."<br />";
?>

The Continue statement terminates execution of the block of statements in a While or For loop and continues execution of the loop with the next iteration:

<?php
echo "<p><b>Example of using the Continue statement:</b><p>";

for ($i=0; $i<=10; $i++) { 
   if (i==3){continue;} 
   echo "The number is ".$i; 
   echo "<br />"; 
} 
?>