Press "Enter" to skip to content

PHP FOREACH Loop Example

In this tutorial, you will learn how to use PHP FOREACH loop by example.

What is FOREACH Loop in PHP?

This loop is used only for arrays. The value of the current array element will be assigned to a variable for each pass, and the array pointer will be moved by one FOREACH loop iteration. This process will be continued until it will reach the last array element.

The syntax of the FOREACH loop is given below:

foreach($array as $a)
{
//codes to be executed
}

Here the value of the current array element will be assigned to $a for every loop iteration.

Now we will give an example for displaying the value of each array element of an array through foreach loop:

<html>
    <body>
        <?php
$a=array("Red","Green","Blue","Yellow");
foreach($a as $t)
{
    echo $t."<br>";
}
        ?>
    </body>
</html>

Output

Red
Green
Blue
Yellow

In this program, an array $a is containing four elements. For each loop iteration, here the value of current array element is assigning to the variable $t, an array pointer is moving by one and displaying the value of each array element until the last array element is found.

Here we are writing a program for using a foreach loop on an associative array in PHP:

In this program, we are using an associative array here $a with 3 keys/values and we are printing the value of each array element through a foreach loop and a print_r function.

<html>
    <body>
        <?php
$a=array("name"=>"amit","address"=>"habra","salary"=>"90000");
foreach($a as $t)
{
    print_r($t);
}
        ?>
    </body>
</html>

Output

amithabra90000

Now we will write another example for foreach loop on a user-defined function:

In this program, we are using a user-defined function f1, and the function body is containing an array $a with four elements, and we are displaying the value of each array element using a foreach loop.

<html>
    <body>
        <?php
function f1()
{
    $a=array("Kolkata","Delhi","Dumdum","Pune");
    foreach($a as $t)
    {
        echo $t."<br>";
    }
}
f1();
        ?>
    </body>
</html>

Output

Kolkata
Delhi
Dumdum
Pune

Hereafter function call, the function body, will be executed and the value of each array element will be displayed in separate lines with a statement echo $t.”<br>”.

Now we are writing a program, where we are passing an array ($a) as an argument to a function here f1:

This program is containing a user-defined function f1, and an array is passing as an argument to the function. The function is containing a foreach loop, through this loop we are displaying the value of each array element with an echo statement. When a function is called, then function body will be executed.

<html>
    <body>
        <?php
function f1($a)
{
    foreach($a as $t)
    {
        echo $t."<br>";
    }
}
$a1=array("apple","banana","orange"); //array is containing 4 element with values as "apple","banana" and "orange"
f1($a1);//calling function by passing an array
        ?>
    </body>
</html>

Output

apple
banana
orange

This program is displaying the results after calling the function.