PHP foreach Loop
The foreach loop in PHP is a powerful and convenient way to iterate over arrays and objects. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively. In this article, we will explore the foreach loop in detail, including its syntax, use cases, variations, and practical examples.
The foreach loop in PHP is specifically designed for iterating over arrays and objects. Unlike traditional loops (like for or while), foreach automatically manages the iteration, which simplifies the code and reduces the potential for errors.
The key advantages of using foreach include:
- Easy to read and write.
- Automatically handles both keys and values in associative arrays.
- Unlike for loops, you don’t need to manually manage index counters.

Basic Syntax of foreach Loop
The basic syntax of the foreach loop is:
foreach( $array as $element ) {
// PHP Code to be executed
}
or, to access both keys and values:
foreach( $array as $key => $element) {
// PHP Code to be executed
}
Examples of foreach Loop
1. foreach Loop with Indexed Arrays
Indexed arrays use numerical keys. The foreach loop can iterate through each element easily.
<?php
$arr = [10, 20, 30, 40, 50];
foreach ($arr as $element) {
echo $element . PHP_EOL;
}
?>
Output
10 20 30 40 50
2. foreach Loop with Associative Arrays
Associative arrays contains the array elements in (key, value) pair format. The foreach loop can handle both keys and values, making it ideal for working with associative arrays.
<?php
$person = [
"name" => "XYZ",
"age" => 30,
"email" => "xyz@example.com"
];
foreach ($person as $key => $value) {
echo "$key: $value" . PHP_EOL;
}
?>
Output
name: XYZ age: 30 email: xyz@example.com
3. foreach Loop with Multidimensional Arrays
Multidimensional arrays (Array of Arrays) stores an another array at each index instead of single element. The foreach loop can be used to iterate over multidimensional arrays.
<?php
$students = [
["name" => "XYZ", "marks" => 85],
["name" => "ABC", "marks" => 92],
["name" => "PQR", "marks" => 78]
];
foreach ($students as $student) {
foreach ($student as $key => $value) {
echo "$key: $value" . PHP_EOL;
}
echo "-----" . PHP_EOL;
}
?>
Output
name: XYZ marks: 85 ----- name: ABC marks: 92 ----- name: PQR marks: 78 -----
4. foreach Loop with Objects
The foreach loop can also be used to iterate over the properties of an object. This is especially useful when working with data retrieved as objects, such as from a database or API.
<?php
class Car {
public $make;
public $model;
public $year;
public function __construct($make, $model, $year) {
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
}
$car = new Car("Toyota", "Corolla", 2020);
foreach ($car as $property => $value) {
echo "$property: $value" . PHP_EOL;
}
?>
Output
make: Toyota model: Corolla year: 2020
5. Break and Continue in foreach Loop
You can use break and continue statements to control the flow within a foreach loop.
- break: Exits the loop entirely.
- continue: Skips the current iteration and moves to the next.
<?php
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach ($arr as $element) {
if ($element % 2 === 0) {
continue;
}
echo $element . PHP_EOL;
if ($element === 7) {
break;
}
}
?>
Output
1 3 5 7