The PHP foreach is another pattern for PHP loops to iterate and expose the data of the iterable expression though the loop. The foreach can also display the element keys during the loop.

The syntax of PHP foreach would be written like the below code.

foreach( $array_table as $value => $key ) {
 // statements 

Also there is another pattern for the PHP foreach.

foreach( $array_table as $value ) {
 // statements 

In the next section I will explain how it works? with an examples Let’s see that.

How Does It Work?

In the below figure you will understand how it works.

PHP Foreach Loop

In this example, I will consider that the element used in the above figure is an array. And I used the foreach to iterate over this array. But what is happening there ?

Firstly, the PHP foreach checks the data existing in the array element so if there is no data it will end the foreach loop directly.

But, if it has data it will do loops and expose the array elements by key and value. And that can be done using the keyword “as” and the double arrow operator “=>“.

So each loop, it will check if the item has data or no so it will execute the statement part with another data according to the array index.

Anyway, in the next section I will focus on how to write embedded foreach with HTML markups.

Embedded PHP Foreach with HTML or JavaScript

The embedded foreach can work with HTML page in separate areas. Let’s do that by code.

  $tuts = array( "PHP", "HTML", "JavaScript" );
  foreach( $tuts as $tutorial ):
<h1> <?= $tutorial; ?> Tutorial</h1>
<?php endforeach; ?>

This code will print all tutorial names inside the array using the HTML heading tag. with a repeated loops.

Using PHP Foreach by Reference

To use the PHP foreach by reference you have to use the & (and) operator in the extracted item. For example.

   $arr = array("item 1", "item 2");
   foreach( $arr as &$value ){
     echo $value;

You can also use the array directly in the foreach loop.

   foreach( $arr as &$value ){
     echo $value;

Foreach with Associative Array

The associative arrays that are using names or numbers in the index fields to be easy in calling. The foreach loop has the ability to extract these keys and values separately during the loop process. Let’s see an example.

   $assoc_array = array(  
      "name" => "Ahmed", 
      "age"  => 20,
      "job"  => "Web Developer"
   foreach( $assoc_array as $key => $value ){
      echo $key . " : " . $value;
      echo "\n";


In this example, I extracted the keys and the values separately and the output would be like the following.

name : Ahmed
age : 20
job : Web Developer

When the foreach do looping in each item it can access the $key and the $value of the array.

No Curly Braces with PHP Foreach

You can use the PHP foreach with no curly braces. That means the foreach will only execute the first statement and ignoring the other lines inside. Let’s see an example.

  $array = array( "Ahmed", "Samy", "Montasser", "Michael" );
  foreach( $array as $arr )
    echo $arr; // execute this line only as in the loop
    echo "This line will not be executed."; // ignore this line


This example will only print the names inside the array according to the first statement. But the foreach will ignore the second statement.

Wrapping Up

In this article, you understood how to use the PHP foreach loop and you saw many examples such as associative array with foreach, one line statement, and indexed array with foreach.