Starting with the basics, a foreach loop is used to iterate over each element in an array. The loop structure starts with the keyword “foreach,” followed by a set of parentheses and a curly brace. Within the parentheses, you specify the array you want to iterate over and a variable name to hold the value of each element in the array as you loop through it.

To further explain the foreach loop, let’s take a look at an example:

$colors = array("red", "green", "blue");

foreach ($colors as $color) {
    echo $color;

The fundamental syntax of a PHP foreach loop can be expressed as shown in the code snippet below.

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

Additionally, there is another structure for the PHP foreach loop.

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

In the following section, I will explain how it operates by providing examples. Let’s take a look.

How Does It Work?

In the below figure, you will understand how it works.

PHP Foreach Loop

The PHP foreach loop works by iterating over each element in an array and executing a block of code for each iteration. It accomplishes this by using a set of predefined syntax rules that define how the loop should behave.

The foreach loop executes by taking the array to iterate over and assigns the value of the first element to a new variable. You define this variable in the loop declaration to hold the value of each element in the array as the loop progresses.

Next, the code inside the loop’s curly braces is executed, which can be any valid PHP code. This code typically performs some operation on the current element of the array, such as printing its value to the screen or manipulating it in some way.

Once the code inside the curly braces executes, the loop proceeds to the next element in the array and assigns the variable to that element’s value. It repeats this process for every element in the array until there are no more elements to iterate over.

One important thing to note is that the variable defined in the loop declaration is a new variable that is created each time the loop runs. It is not a reference to the original array, so any changes made to the variable within the loop will not affect the original array.

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:

  $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 loop.

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

To facilitate easy retrieval, associative arrays use names or numbers as index fields. During iteration with a foreach loop, you can extract keys and values separately. Take the following example into consideration.

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


I separated the keys and values in this instance, resulting in the following output.

name : Ahmed
age : 20
job : Web Developer

During the foreach loop, each item can be accessed and the corresponding $key and $value of the array can also be accessed.

No Curly Braces with PHP Foreach

It is possible to use the PHP foreach loop without curly braces, resulting in the loop only executing the first statement and disregarding any subsequent lines.

The following example will solely display the names contained in the array based on the first statement. The second statement will be disregarded by the foreach loop.

  $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


Wrapping Up

Throughout this article, you have gained an understanding of how to utilize the PHP foreach loop, with various examples provided including associative array with foreach, one line statement, and indexed array with foreach.