PHP Loops

Loops are used in php to perform an action many times. For example, we want to display our name 100 times on a webpage. Instead of typing it 100 times, we can use a loop. The loop will display it 100 times.


A FOR loop requires three parameters:

  • start: the starting point, a value for the counter variable
  • condition: the condition that has to be met to run the loop again
  • increment: the counter has to be incremented (or decremented) before the loop is run again

for(start; condition; increment )
   // Code to repeat

<?php #loops.php
    //     start,  condition, increment 
     for(  $i=1;   $i<=10;    $i++     )
         //display this message
         echo "<p>". $i . ": shahid</p>";

An example script that uses for to print out the 7 times table:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>The 7 Times Table</title> 
      <link rel="stylesheet" type="text/css" href="/~cs1spw/tutorials/blue.css"> 
      <h1>The 7 Times Table</h1> 
for ($i = 1; $i <= 10; $i++) { echo "<li>$i times 7 is ".($i * 7)."</li> "; }
?> </ul> </body> </html>



A WHILE loop will run a piece of code again and again until the condition becomes false. The code will be repeated only while the condition is true.

For a while loop you will need:

  • initialisation expression to be evaluated just once before the loop (start)
  • test expression to be evaluated each time the loop runs (condition)
  • loop body: the block of code to be repeated
  • iteration expression to be run after each loop execution (increment)

Please Note: without an increment you will have an infinite loop: it will never stop on its own. (fortunately modern browsers are able to stop them)

<?php #loops.php
   //Start counting
   $count = 1;

   //loop condition
   while($count <= 10)
     //display a message
     echo "<p>$count: this is a while loop</p>";

 //increment value of count by one $count++; } ?>


A DO-WHILE loop can be used to execute the body at least once (the very first time) and it will have this syntax:

} while(condition)

Here a working sample:

<?php #loops.php
   //set the counter
   $count = 1;

     //display a message
     echo "<p>$count: this is a do-while loop</p>";

 //increment value of count by one $count++; } while($count <= 10)  //condition ?>

PHP For Each Loop

A FOREACH loop will allow you to iterate over elements in an array. You can use a foreach loop accessing each key of an array or both keys and values.

Accessing keys:

foreach ($array as $key){
    echo “$key”;

<?php #loops.php
   //the array
   $colours = array("red", "green", "blue" );

   //foreach loop
   foreach($colours as $colour)
     //display a message for each item
     echo "<p>$colour</p>";


Accessing keys and values:

foreach ($array as $key => $value) {
    echo “$key : $value”;

<?php #loops.php
   //an associative array
   $colours = array("red" => "#F00", 
"green" => "#0F0",
"blue" => "#00F" ); //foreach loop foreach($colours as $colour => $code) { //accessing keys and values echo "<p>The hexadecimal code for <strong>$colour</strong> is: $code.</p>"; } ?>

Click here to read about PHP functions.