PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The foreach
loop is a control structure in PHP that is specifically designed to loop over arrays. It provides an easy way to iterate over each element in an array without having to worry about managing an index variable or checking the length of the array.
Here's the basic syntax of a foreach
loop:
foreach ($array as $value) { // code to be executed; }
In this syntax:
$array
is the array that you want to loop over.$value
is the current element's value.Example 1: Basic Usage
$fruits = array("Apple", "Banana", "Cherry"); foreach ($fruits as $fruit) { echo $fruit; }
In this example, the foreach
loop iterates over the $fruits
array. On each iteration, it assigns the current element's value to $fruit
and then executes the echo
statement.
Example 2: Key/Value Pairs
foreach
also supports iterating over associative arrays (arrays with key/value pairs). Here's how you can do it:
$person = array("name" => "John", "age" => 30, "city" => "New York"); foreach ($person as $key => $value) { echo "Key=" . $key . ", Value=" . $value; }
In this example, $key
holds the current element's key and $value
holds the current element's value. The foreach
loop iterates over each element in the $person
array, assigns the key and value to $key
and $value
respectively, and then executes the echo
statement.
Note: The foreach
loop works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.
How to iterate over arrays with foreach in PHP:
foreach
with a simple array:<?php $numbers = [1, 2, 3, 4, 5]; foreach ($numbers as $number) { echo $number . "\n"; }
Using foreach with associative arrays in PHP:
foreach
:<?php $person = [ 'name' => 'John', 'age' => 30, 'city' => 'New York', ]; foreach ($person as $key => $value) { echo "$key: $value\n"; }
Multidimensional arrays and foreach in PHP:
foreach
:<?php $matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; foreach ($matrix as $row) { foreach ($row as $value) { echo $value . " "; } echo "\n"; }
Key and value access in PHP foreach loop:
foreach
loop:<?php $colors = [ 'red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF', ]; foreach ($colors as $color => $hex) { echo "Color: $color, Hex: $hex\n"; }
Modifying array elements with foreach in PHP:
foreach
:<?php $numbers = [1, 2, 3, 4, 5]; foreach ($numbers as &$number) { $number *= 2; } unset($number); // Unset the reference to avoid unexpected behavior print_r($numbers);
Using foreach with objects in PHP:
foreach
:<?php class Person { public $name = 'John'; public $age = 30; public $city = 'New York'; } $person = new Person(); foreach ($person as $key => $value) { echo "$key: $value\n"; }
Skipping and breaking out of foreach loop in PHP:
continue
to skip an iteration and break
to exit the loop:<?php $numbers = [1, 2, 3, 4, 5]; foreach ($numbers as $number) { if ($number % 2 === 0) { continue; // Skip even numbers } echo $number . "\n"; if ($number === 3) { break; // Stop the loop when 3 is encountered } }
Differences between foreach and for loop in PHP:
foreach
and for
loops:<?php // Using for loop $numbers = [1, 2, 3, 4, 5]; $count = count($numbers); for ($i = 0; $i < $count; $i++) { echo $numbers[$i] . "\n"; } // Using foreach loop foreach ($numbers as $number) { echo $number . "\n"; }