PHP Foreach Loop

The foreach loop in PHP is used to iterate through each element in an array or the properties of an object. It is a convenient way to handle collections in PHP.

Tutorials dojo strip



The foreach Loop for Arrays

The foreach loop is most commonly used to loop through the elements of an array.

Example: Looping Through Motorcycle Brands

$motorcycleBrands = array("Suzuki", "Yamaha", "Honda", "BMW");

foreach ($motorcycleBrands as $brand) {
  echo "Brand: $brand <br>";
}

Explanation:

  • The array $motorcycleBrands contains several motorcycle brand names.
  • During each iteration, the current value of the array is assigned to the variable $brand and can be used within the loop.
  • The loop continues until all brands are processed.




Working with Keys and Values

When using associative arrays, you can access both the keys and values in the loop.

Example: Brand Names and Countries

$brandOrigins = array("Suzuki" => "Japan", "BMW" => "Germany", "Honda" => "Japan", "Yamaha" => "Japan");

foreach ($brandOrigins as $brand => $origin) {
  echo "$brand originates from $origin <br>";
}

Explanation:

  • $brand holds the array key (e.g., "Suzuki").
  • $origin contains the corresponding value (e.g., "Japan").
  • This allows you to access both the key and value of each element in the array.




The foreach Loop for Objects

The foreach loop can also be used to iterate through an object’s properties.

Example: Motorcycle Details

class Motorcycle {
  public $brand;
  public $model;
  public function __construct($brand, $model) {
    $this->brand = $brand;
    $this->model = $model;
  }
}

$myBike = new Motorcycle("Yamaha", "R1");

foreach ($myBike as $property => $value) {
  echo "$property: $value <br>";
}

Explanation:

  • The loop iterates through each property ($property) and its corresponding value ($value) in the object $myBike.




Controlling the Loop: Break and Continue

You can control the execution of the loop using the break and continue statements.

The Break Statement

The break statement stops the loop prematurely:

$motorcycleBrands = array("Suzuki", "Yamaha", "Honda", "BMW");

foreach ($motorcycleBrands as $brand) {
  if ($brand == "Honda") break;
  echo "Brand: $brand <br>";
}

Explanation:

  • The loop stops as soon as it encounters "Honda".

The Continue Statement

The continue statement skips the current iteration and moves to the next:

$motorcycleBrands = array("Suzuki", "Yamaha", "Honda", "BMW");

foreach ($motorcycleBrands as $brand) {
  if ($brand == "Honda") continue;
  echo "Brand: $brand <br>";
}

Explanation:

  • The loop skips "Honda" and continues with the remaining brands.




Modifying Array Items by Reference

By default, changes to an array item inside a foreach loop do not affect the original array. However, you can modify the original array using the & symbol.

Example: Changing a Brand Name

$motorcycleBrands = array("Suzuki", "Yamaha", "Honda", "BMW");

foreach ($motorcycleBrands as &$brand) {
  if ($brand == "Honda") $brand = "Ducati";
}

var_dump($motorcycleBrands);

Explanation:

  • The & symbol makes changes to the array items directly affect the original array.
  • In this case, "Honda" is replaced with "Ducati".




Alternative Syntax

The foreach loop can also be written using the endforeach keyword.

Example: Loop Through Brands

$motorcycleBrands = array("Suzuki", "Yamaha", "Honda", "BMW");

foreach ($motorcycleBrands as $brand) :
  echo "$brand <br>";
endforeach;
Tutorials dojo strip
Scroll to Top