The foreach loop is used to access each element of an array. It is especially useful when we do not know the number of elements in an array.
A foreach loop automatically reads each element of an array one by one.
<?php
$colors = ["Red", "Green", "Blue"];
foreach ($colors as $color) {
echo $color . "<br>";
}
?>
Red
Green
Blue
Here, $color receives each array value
during every iteration.
An indexed array stores values using numeric indexes. Foreach reads the values in their iteration order.
<?php
$fruits = ["Apple", "Mango", "Banana", "Orange"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>
Apple
Mango
Banana
Orange
We do not need to manage an index variable manually.
An associative array stores data using named keys. We can access both keys and values with foreach.
<?php
$student = [
"name" => "Rahul",
"age" => 20,
"course" => "PHP"
];
foreach ($student as $key => $value) {
echo $key . ": " . $value . "<br>";
}
?>
name: Rahul
age: 20
course: PHP
The variable $key stores the current key,
while $value stores its corresponding value.
The general syntax for accessing keys and values is:
foreach ($array as $key => $value) {
// Statements
}
Example:
<?php
$marks = [
"English" => 85,
"Math" => 90,
"Computer" => 95
];
foreach ($marks as $subject => $mark) {
echo $subject . " = " . $mark . "<br>";
}
?>
English = 85
Math = 90
Computer = 95
This approach is useful for displaying student marks, product prices, employee details, and other records.
We can also retrieve the numeric index and value of an indexed array.
<?php
$subjects = ["English", "Hindi", "Math", "Science"];
foreach ($subjects as $index => $subject) {
echo "Index " . $index . ": " . $subject . "<br>";
}
?>
Index 0: English
Index 1: Hindi
Index 2: Math
Index 3: Science
Remember that array indexes do not always start at zero. If an array has custom numeric keys, foreach uses those actual keys.
By default, the loop variable receives a copy of each array value. Changing that variable does not update the original array.
<?php
$numbers = [10, 20, 30];
foreach ($numbers as $number) {
$number = $number + 5;
}
print_r($numbers);
?>
Array
(
[0] => 10
[1] => 20
[2] => 30
)
To update the original array values, use a reference
with &.
<?php
$numbers = [10, 20, 30];
foreach ($numbers as &$number) {
$number = $number + 5;
}
unset($number);
print_r($numbers);
?>
Array
(
[0] => 15
[1] => 25
[2] => 35
)
Important: Always use
unset($number) after a reference-based
foreach loop to remove the reference. Otherwise, the
variable can remain linked to the last array element.
A foreach loop inside another foreach loop is called a nested foreach loop. It is useful for multidimensional arrays.
<?php
$students = [
["name" => "Rahul", "marks" => 85],
["name" => "Amit", "marks" => 90],
["name" => "Priya", "marks" => 95]
];
foreach ($students as $student) {
foreach ($student as $key => $value) {
echo $key . ": " . $value . "<br>";
}
echo "<hr>";
}
?>
name: Rahul
marks: 85
name: Amit
marks: 90
name: Priya
marks: 95
The outer loop accesses each student, and the inner loop accesses the details of that student.
| For Loop | Foreach Loop |
|---|---|
| Uses initialization, condition, and update. | Iterates over array elements. |
| Often used when the iteration count is known. | Useful for processing array values. |
| Can be used with numeric counters. | Can access values and keys directly. |
| Index management may be required. | No manual counter is needed for normal array iteration. |
For loop example:
<?php
$numbers = [10, 20, 30];
for ($i = 0; $i < count($numbers); $i++) {
echo $numbers[$i] . "<br>";
}
?>
Foreach example:
<?php
$numbers = [10, 20, 30];
foreach ($numbers as $number) {
echo $number . "<br>";
}
?>
Suppose we want to display a list of products and their prices. An associative array and foreach loop make this task simple.
<?php
$products = [
"Keyboard" => 500,
"Mouse" => 300,
"Monitor" => 7000,
"Printer" => 5500
];
echo "<h3>Product List</h3>";
foreach ($products as $product => $price) {
echo $product . " - Rs. " . $price . "<br>";
}
?>
Product List
Keyboard - Rs. 500
Mouse - Rs. 300
Monitor - Rs. 7000
Printer - Rs. 5500
1. What is the main purpose of a foreach loop?
2. Which syntax accesses both the key and value?
3. What should you do after using a reference variable in foreach?