Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

Foreach Loop

The foreach Loop on Arrays

The primary use of the foreach loop is to iterate through the items in an array.

Example

Iterate through the elements of an indexed array:

 $colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
 echo "$x <br>";
}

In each iteration of the loop, the value of the current array element is assigned to the variable $x. The loop continues until it reaches the last element of the array.

Keys and Values

The array above is an indexed array, where the first item has the key 0, the second has the key 1, and so on.

In contrast, associative arrays use named keys that you assign to them. When iterating through associative arrays, you might want to keep both the key and the value.

You can do this by specifying both the key and value in the foreach definition, as demonstrated below:

Example

Output both the key and the value from the $members array:

$members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach ($members as $x => $y) {
 echo "$x : $y <br>";
}

The foreach Loop on Objects

The foreach loop can also be used to iterate through the properties of an object:

Example

Display the property names and values of the $myCar object:

class Car {
 public $color;
 public $model;
 public function __construct($color, $model) {
    $this->color = $color;
    $this->model = $model;
 }
}
$myCar = new Car("red", "Volvo");
foreach ($myCar as $x => $y) {
 echo "$x: $y <br>";
}

The break Statement

Using the break statement, we can exit the loop even if it hasn’t reached the end.

Example

Terminate the loop if $x is “blue”:

 $colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
 if ($x == "blue") break;
 echo "$x <br>";
}

The continue Statement

The continue statement allows us to skip the current iteration and proceed to the next one.

Example

Skip to the next iteration if $x is “blue”:

$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
 if ($x == "blue") continue;
 echo "$x <br>";
}

Foreach Byref

When iterating through the array items, any modifications made to an array item will, by default, NOT impact the original array.

Example

By default, modifying an array item will not affect the original array.

$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
 if ($x == "blue") $x = "pink";
}
var_dump($colors);

However, by using the & character in the foreach declaration, the array item is assigned by reference, meaning any changes made to the array item will also affect the original array.

Example

By assigning the array items by reference, modifications will impact the original array.

$colors = array("red", "green", "blue", "yellow");
foreach ($colors as &$x) {
 if ($x == "blue") $x = "pink";
}
var_dump($colors);

Alternative Syntax

The foreach loop syntax can also be expressed using the endforeach statement, as shown here:

Example

Iterate through the elements of an indexed array:

$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) :
 echo "$x <br>";
endforeach;