๐Ÿ” PHP Control Flow & Logic
Estimated reading: 3 minutes 29 views

๐Ÿ”„ PHP Iterables โ€“ Looping Through Arrays, Objects & Generators

Master the concept of iterables in PHP, including arrays, objects, and the powerful Traversable interface.


๐Ÿงฒ Introduction โ€“ What Are Iterables in PHP?

An iterable in PHP refers to any value that can be looped over using foreach. This includes traditional arrays, objects that implement the Traversable interface (like Iterator and Generator), and anything defined as iterable in type declarations.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • What qualifies as an iterable in PHP
  • Differences between arrays and objects as iterables
  • How to create custom iterable classes
  • How generators simplify custom iteration
  • Iterable type hinting (PHP 7.1+)

โœ… Basic Iterable โ€“ Array

$names = ["Alice", "Bob", "Charlie"];

foreach ($names as $name) {
    echo $name . "<br>";
}

๐Ÿ” Output:

Alice
Bob
Charlie

๐Ÿ“˜ Arrays are the most common and default iterable in PHP.


๐Ÿง‘โ€๐Ÿ’ป Iterable Object โ€“ Using Traversable

Objects can be made iterable by implementing the Iterator or IteratorAggregate interface.

Example โ€“ Using IteratorAggregate

class Team implements IteratorAggregate {
    private $members = ["Alice", "Bob", "Charlie"];

    public function getIterator(): Traversable {
        return new ArrayIterator($this->members);
    }
}

$team = new Team();

foreach ($team as $member) {
    echo "$member <br>";
}

๐Ÿ” Output:

Alice
Bob
Charlie

๐Ÿ“˜ Traversable is the base interface for all iterable objects.


โš™๏ธ Iterable Type Hinting (PHP 7.1+)

function printItems(iterable $data) {
    foreach ($data as $item) {
        echo "$item <br>";
    }
}

printItems(["red", "green", "blue"]);

๐Ÿ“˜ Use iterable as a parameter or return type to accept both arrays and Traversable objects.


๐Ÿ” Generator Function โ€“ Lightweight Iterables

Generators let you yield values lazily without creating arrays manually.

function countToThree() {
    yield 1;
    yield 2;
    yield 3;
}

foreach (countToThree() as $number) {
    echo $number . " ";
}

๐Ÿ” Output: 1 2 3

๐Ÿ“˜ Memory-efficient way to build iterables on the fly.


๐Ÿ”„ Comparing Iterables in PHP

TypeIterable?Description
arrayโœ… YesNative PHP iterable
Generatorโœ… YesYields values using yield
Iteratorโœ… YesInterface that supports iteration
Traversableโœ… YesBase for all objects usable in foreach
stdClassโŒ NoNot iterable unless cast to array
stringโŒ NoNot iterable in foreach

๐Ÿง  Best Practices

  • โœ… Use iterable type hints when accepting arrays or generators
  • โœ… Use generators for performance when working with large datasets
  • โœ… Implement IteratorAggregate for simple custom iterable classes
  • โŒ Avoid using non-iterable types in foreach (e.g., strings, integers)

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

PHP iterables allow you to loop through data efficiently and flexibly, whether it’s an array, a generator, or a custom object.

๐Ÿ” Key Takeaways:

  • All arrays and Traversable objects are iterable
  • Use foreach to iterate over any iterable
  • Type hint with iterable for broad compatibility
  • Use generators to improve memory usage

โš™๏ธ Real-World Use Cases:
Looping over datasets, building collections, processing files, implementing custom data structures.


โ“ Frequently Asked Questions (FAQs)

โ“ What data types are iterable in PHP?
โœ… Arrays, Generator, and any object implementing Traversable (Iterator, IteratorAggregate).

โ“ What is the difference between Traversable and iterable?
โœ… Traversable is an interface. iterable is a broader type hint introduced in PHP 7.1 that accepts arrays and any Traversable.

โ“ Can I use iterable as a return type?
โœ… Yes, like this:

function getData(): iterable { return [1, 2, 3]; }

โ“ Are strings iterable in PHP?
โŒ No. foreach cannot iterate over strings unless converted to arrays.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”„ PHP โ€” Iterables

Or Copy Link

CONTENTS
Scroll to Top