Learn with Yasir
Share Your Feedback

PHP foreach Loop Tutorial with Syntax and Examples


earn how to iterate over arrays using the foreach loop in PHP. See real-world examples, syntax breakdown, and use cases for better PHP code.

πŸ”„ PHP foreach Loop

The foreach loop is used to iterate over arrays or objects in PHP. It’s the easiest way to go through each element in an array.


βœ… Basic Syntax

There are two main ways to use foreach:

// Value only
foreach ($array as $value) {
    // Code using $value
}
// Key and value
foreach ($array as $key => $value) {
    // Code using $key and $value
}
  • $value holds the current item in the array.
  • $key holds the current key (index or name) of the item.

πŸ›  Modifying Array Items

If you want to change the array values directly, use & to reference the value:

foreach ($array as &$value) {
    $value = $value * 2;
}
unset($value); // Always unset to avoid unexpected bugs!

for more details about unset, see Why You Should Always Use unset($value); After a foreach Loop in PHP β€”

🚨 Important:

  • foreach only works with arrays and objects.
  • Always unset($value) after using a reference to prevent accidental changes later.

πŸ” Examples

πŸ”Έ Example 1: Simple foreach Loop

<?php
$colors = array("red", "green", "blue");

foreach ($colors as $color) {
    echo "Color: $color <br>";
}
?>

Output:

Color: red  
Color: green  
Color: blue

πŸ”Έ Example 2: Using Keys and Values

<?php
$prices = array("apple" => 50, "banana" => 20);

foreach ($prices as $item => $cost) {
    echo "$item costs $cost rupees<br>";
}
?>

Output:

apple costs 50 rupees  
banana costs 20 rupees

πŸ”Έ Example 3: Styling with HTML

<!DOCTYPE html>
<html>
<head><title>Colors</title></head>
<body>

<?php
$colors = array("red", "green", "blue");

foreach ($colors as $color) {
    echo "<h2 style='background-color:$color;'>$color</h2>";
}
?>

</body>
</html>

πŸ”Έ Example 4: Nested Arrays

<?php
$matrix = [
    [1, 2],
    [3, 4]
];

foreach ($matrix as $row) {
    foreach ($row as $item) {
        echo "$item ";
    }
    echo "<br>";
}
?>

Output:

1 2  
3 4

πŸ”Έ Example 5: Inline Array

<?php
foreach (array("a", "b", "c") as $letter) {
    echo $letter . "<br>";
}
?>

🧠 Key Tips

  • Use foreach when working with arrays β€” it’s simpler and safer than a for loop.
  • If you don’t need the key, use the shorter form: foreach ($arr as $value).
  • Always be careful when modifying array elements by reference (use unset() afterward).

Would you like an infographic or diagram explaining how foreach works step-by-step?

Further reading:


🧠 Practice & Progress

Explore More Topics

PHP Basics

More ...