Learning PHP 101 – For & Foreach Loops

Programmers often have to repeat blocks of code a given number of times or until a certain condition is met and we can achieve this by either typing out each iteration like so:

<?php
    echo "1";
    echo "2";
    echo "3";
    echo "4";
    // and so on
?>

or we can use a for loop.

<?php
    for ($i = 0; $i < 10; $i++) {
        echo "<p>$i</p>";
    }
?>

I think you can agree that loops are infinitely better than echo‘ing out each block of code separately. It also means that if you need to change the output in anyway – (perhaps altering the HTML output) – you only have to do it once.

There are a number of different types of loops commonly used in PHP, the for loop comes in two forms.

For Loop

The for loop is ideal for running the same code over and over when you know ahead of time the total number of times you need to run the code. The for keyword tells PHP its about to loop.

Syntax

<?php
    for (init counter; test counter; increment counter) {
        // output
    }
?>

Inside the set of parentheses () we have 3 points that set the conditions of the loop seperated by semicolons (;).

  1. init counter – is the value the loop will start from. Example $i = 1.
  2. test counter – is is the condition that must be met in order for the loop to end. Example $i < 10.
  3. increment counter – determines what should happen for each iteration of the loop. Example $i++ which is short for $i = $i + 1 will increment the loop variable by 1.
If you wanted to increment by 2 then you would write $i = $i + 2;

If the test counter is true then the code block inside the curly braces {} will run and continue to do so until the test counter is false and the loop will then exit.

for-loop-diagram

Figure 1: Illustration of a for loop in PHP.

Foreach Loops

The foreach loop is used to iterate over each element of an array.

Syntax

foreach ($array as $value) {
     code to be executed;
 }

Remember in part 3 we talked about arrays and how we can echo out different items within the array like so:

<?php
    $notes = array(
        "sticky note 1",   // [0]
        "sticky note 2",   // [1]
        "sticky note 3");  // [2]

    echo $notes[1]; // outputs "new sticky note"
?>

Well what if we wanted to echo out all the items within the array. This is where the foreach loop comes in to play.

<?php
    $notes = array(
        "sticky note 1",   // [0]
        "sticky note 2",   // [1]
        "sticky note 3");  // [2]


    foreach($notes as $note) {
        echo "<p>$note</p>"; // output
    }
?>

This loop will now output each “sticky note” and if we ever wanted to update or change the formatting of our “sticky notes” then we can simply edit the output once rather than for each individual item.

Summarize

  • The for loop is used to loop through a block of code a specified number of times.
  • The foreach loop is used to loop through array items and execute a block of code for each item.

Next: we will look at While and Do-While Loops!

In Category: PHP tutorials

Matthew Horne

I am a web developer from the United kingdom who taught himself PHP and JavaScript and continues to build on those skills as well as learn new skills.

Show 0 Comments
No comments yet. Be the first.

Leave a Comment