codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

13 / 56 topics
7Control Structures8If, Else, and Elseif Statements9Switch Case Statement10For Loop11While Loop12Do While Loop13Foreach Loop
Tutorials/PHP/Foreach Loop
🐘PHP

Foreach Loop

Updated 2026-05-15
10 min read

Foreach Loop

Introduction

In PHP, the foreach loop is a powerful tool for iterating over arrays and objects. It provides a clean and efficient way to access each element in an array or object without needing to manually manage indices or keys. This tutorial will guide you through the basics of using the foreach loop, including how to iterate over both arrays and objects.

Concept

The foreach loop is designed specifically for iterating over arrays and objects. It allows you to access each element in a collection one by one. The basic syntax for a foreach loop is as follows:

foreach ($array as $value) {
    // Code to be executed for each element
}

In this syntax:
- `$array` is the array or object you want to iterate over.
- `$value` is a variable that will hold the value of the current element in the iteration.

For objects, you can also access both the key and the value:

```php
foreach ($object as $key => $value) {
    // Code to be executed for each property
}

In this case:
- `$key` is a variable that will hold the key or property name of the current element.
- `$value` is a variable that will hold the value of the current element.

## Examples

### Iterating Over Arrays

Let's start with an example of iterating over an array. Suppose you have an array of fruits:

```php
$fruits = ['apple', 'banana', 'cherry'];

foreach ($fruits as $fruit) {
    echo $fruit . "\n";
}

<OutputBlock>
{`apple
banana
cherry`}
</OutputBlock>

In this example, the `foreach` loop iterates over each element in the `$fruits` array. The variable `$fruit` takes on the value of each element in turn, and the `echo` statement outputs the name of each fruit.

### Iterating Over Associative Arrays

Associative arrays are arrays where each element is associated with a key. You can access both the key and the value using the `foreach` loop:

```php
$person = [
    'name' => 'John',
    'age' => 30,
    'city' => 'New York'
];

foreach ($person as $key => $value) {
    echo "$key: $value\n";
}
Output
name: John
age: 30
city: New York

In this example, the foreach loop iterates over each element in the $person associative array. The variable $key takes on the key of each element, and the variable $value takes on the value of each element.

Iterating Over Objects

PHP also allows you to iterate over objects using the foreach loop. Suppose you have a simple object:

class Person {
    public $name = 'John';
    public $age = 30;
    public $city = 'New York';
}

$person = new Person();

foreach ($person as $key => $value) {
    echo "$key: $value\n";
}
Output
name: John
age: 30
city: New York

In this example, the foreach loop iterates over each property of the $person object. The variable $key takes on the name of each property, and the variable $value takes on the value of each property.

What's Next?

Now that you have a good understanding of how to use the foreach loop in PHP, you might want to explore more advanced control structures and functions. In the next section, we will dive into "Functions in PHP," where you will learn how to define and use custom functions to encapsulate reusable code.

Stay tuned for more tutorials on codingstuff.io!


PreviousDo While LoopNext Functions in PHP

Recommended Gear

Do While LoopFunctions in PHP