Jump to content


- - - - -

P9: Loops


  • Please log in to reply
No replies to this topic

#1 DraxxTV



  • SuperGeek
  • PipPip
  • 14 posts

Posted 06 July 2014 - 07:58 PM

In this tutorial we will be going over some of the different loops which you may  need to use while using php. Each type of loop is useful of specific tasks and understanding which one will be best for your needs can be a challenge. I will do my best to explain each of them so that you try and get your head around it. PHP supports three kinds of loops: for, while, and foreach. The while loop is similar to for, but it’s used most frequently when retrieving values from a database or reading from a text file.

The For Loop

The for loop is designed to perform a section of code a specific amount of times. The way this is formated is like follows

for (initial expression; condition; closing expression) {
    code to be executed;

In the next example, I will show you how you can run a block of code which will count to 10 and stop when it gets there. Each time the code runs, it will increment the counter.

for ($i = 1; $i <= 10; $i++) {
    echo "This number is : " . $i . "<br>";

If you run this, you should see

This number is : 1
This number is : 2
This number is : 3
This number is : 4
This number is : 5
This number is : 6
This number is : 7
This number is : 8
This number is : 9
This number is : 10

One very useful example of how this could be used which I found here is a code snippet which would look like as follows:


The code used to create this multiplication table is here:

Attached File  multiplication table.JPG   35.27KB   0 downloads

echo "<h1>Multiplication table</h1>";
echo "<table border=2 width=50%";

for ($i = 1; $i <= 9; $i++ ) { //this is the outer loop
    echo "<tr>";
    echo "<td>".$i."</td>";

    for ( $j = 2; $j <= 9; $j++ ) { // inner loop
        echo "<td>".$i * $j."</td>";

    echo "</tr>";

echo "</table>";

The While Loop

The while loop also works in a similar way. This is formatted like so:

while (condition){
    code to be executed;

An example of how this code would work is here:

while ($i <= 10) { // Output values from 0 to 10
    echo "This number is ". $i ."<br />";

One example of how this could be used would be if you wanted a form to have an area for you to select your age. This could be used to output the following:

Attached File  Date picker.JPG   9.45KB   0 downloads

The code which you could use to do this is

$month_array = array( "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sept", "Oct", "Nov", "Dec");
echo "<select name='day' style='width:50px;'>";
$day = 1;
while ( $day <= 31 ) {
    echo "<option value=".$day.">".$day."</option>";
echo "</select>";

echo "<select name='month' style='width:50px;'>";
    foreach($month_array as $monthNumber =>$month){
    echo "<option value=".$monthNumber.">".$month."</option>";
echo "</select>";

echo "<select name='year'style='width:50px;'>";
$i = 2013;
while ( $i >= 1900 ) {
    echo "<option value=".$i.">".$i."</option>";
echo "</select>";

The Do While Loop

The do while statement is pretty much the same as the while statement however this time the condition is tested at the end of the loop rather than at the beginning. This can be useful as it means your code will run at least once regardless. The statement is formated like such:

    code to be executed;
}while (condition);

As I have already covered the while loop above, I will not talk about the things you can do with it as they are essentially the same :)

The Foreach Loop

During an example inside the while loop, I included a foreach loop. A foreach loop is when working with arrays. They are used to access parts programmatically within an array. It basically works by saying for each item inside this array do the following. An example of the structure for this would be one of the two following ways:

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

When looking at a foreach loop, it can help to look at then like this

Attached File  foreach loop.JPG   18.5KB   0 downloads

The use for the foreach loop I used before was this:

$month_array = array( "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sept", "Oct", "Nov", "Dec");
echo "<select name='month' style='width:50px;'>";
foreach($month_array as $monthNumber =>$month){
    echo "<option value=".$monthNumber.">".$month."</option>";
echo "</select>";

Continue, Break, and Array Pointers

When you are playing about with all of these loops, there may come a time where you feel you need to break out of a loop to continue with the reset of the script. Depending on what kind of loop you are doing, there are different ways you can do this. I have found a very good video which explains this very well. This video has been done by Michael Lively and I hope this helps

Also tagged with one or more of these keywords: PHP4Beginners