PHP Loop Constructs

Loop constructs are used in programming to execute the same program statement or block of code for a given number of times (or iterations), or until some exit condition has been satisfied. An exit condition is something that must be true in order for the program to stop executing the code contained within the body of the loop, and continue on to the next part of the program. PHP has a number of loop constructs, which are briefly described below.

The short program below demonstrates the use of the while loop construct.


<html>
  <body>

<?php
  $num=1;
  while($num <= 10)
  {
    // this code comprises the "body" of the loop
    echo $num . " squared is: " . $num * $num . "<br>";
    $num++;
  }
?>

  </body>
</html>


Enter the code above into a text editor and save it with the filename "while.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser's address bar:


http://localhost/while.php


You should see output that is something like the screenshot below.


The output from while.php

The output from while.php


The short script below demonstrates the use of the do-while loop construct. Note that this script does exactly the same thing as the previous script. It just does it in a slightly different way (note that for this version of the script, if you set the initial value of $num to a value greater than 10, the loop will still execute once).


<html>
  <body>

<?php
  $num=1;
  do
  {
    echo $num . " squared is: " . $num * $num . "<br>";
    $num++;
  }
  while($num <= 10)
?>

  </body>
</html>


Enter the code above into a text editor and save it with the filename "do_while.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser's address bar:


http://localhost/do_while.php


You should see output that is something like the screenshot below. Note that the output for this script is exactly the same as for the previous script.


The output from do_while.php

The output from do_while.php


If you change the initial value of $num to 12 the loop will execute once even though the loop's exit condition has already been met (i.e. $num has a value of greater than 10), and will produce the following output:


The output from do_while.php ($num initialised to 12)

The output from do_while.php ($num initialised to 12)


The short script below demonstrates the use of the for loop construct. Note that this script does exactly the same thing as the previous two scripts. In this case, however, we are using the loop counter variable ($counter) to determine what values to output to the screen. The for loop is often referred to as a counting loop, because it allows you to specify exactly how many times the code within the loop body will execute.


<html>
  <body>

<?php
  for ($counter = 1; $counter <= 10; $counter++)
  {
    echo $counter . " squared is: " . $counter * $counter . "<br>";
  }
?>

  </body>
</html>


Enter the code above into a text editor and save it with the filename "for.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser's address bar:


http://localhost/for.php


You should see output that is something like the screenshot below. Note that the output for this script is exactly the same as for the first two scripts.


The output from for.php

The output from for.php


The short script below demonstrates the use of the foreach loop construct. Note that this script does not require an exit condition or a loop counter variable. It simply processes the array elements in the order in which they are stored within the array, and stops executing after it has dealt with the last array element.


<html>
  <body>

<?php
  $calendar_month = array("January", "February", "March", "April", "May", "June");

  echo "The first six months of the year are:<br><br>";

  foreach ($calendar_month as $month)
  {
    echo $month . "<br>";
  }
?>

  </body>
</html>


Enter the code above into a text editor and save it with the filename "foreach.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser's address bar:


http://localhost/foreach.php


You should see output that is something like the screenshot below.


The output from foreach.php

The output from foreach.php