Understanding PHP Iterables: Iterate Smarter, Code Cleaner
Last updated 3 months, 4 weeks ago | 316 views 75 5

Introduction: Why PHP Iterables Matter
When working with data collections in PHP—like arrays, objects, or generators—you often need to loop through each item to display, manipulate, or compute something.
Before PHP 7.1, you typically used arrays or objects implementing Traversable. But these weren't easily enforceable as type hints. Enter PHP Iterables—a unified way to handle any traversable collection, whether it's an array or object.
Using iterable
simplifies function definitions, enhances readability, and ensures better code flexibility.
What Are PHP Iterables?
In PHP, an iterable is any value that can be looped over using a foreach
loop. This includes:
-
Arrays
-
Objects implementing
Traversable
(likeGenerator
,Iterator
,IteratorAggregate
)
PHP introduced the iterable
pseudo-type in version 7.1 to allow type hinting for any loopable structure.
Declaring Iterable Parameters and Return Types
✅ Function with iterable
Parameter
function printItems(iterable $items) {
foreach ($items as $item) {
echo $item . PHP_EOL;
}
}
-
This function accepts arrays or Traversable objects
-
Ensures the input can be safely looped
Function Returning an Iterable
function getItems(): iterable {
return [1, 2, 3];
}
You can also return generators or any traversable.
Looping Over Iterables
$names = ["Alice", "Bob", "Charlie"];
printItems($names);
// With a generator
function nameGenerator(): iterable {
yield "Alice";
yield "Bob";
yield "Charlie";
}
printItems(nameGenerator());
Both examples above work seamlessly because they return iterables.
Iterables vs Arrays vs Traversable
Feature | Iterable | Array Only | Traversable Only |
---|---|---|---|
Introduced in | PHP 7.1 | Built-in | PHP 5.0 |
Loopable via foreach |
✅ | ✅ | ✅ |
Supports Generators | ✅ | ❌ | ✅ |
Type hint support | ✅ (iterable ) |
✅ (array ) |
✅ (Traversable ) |
Flexible input types | ✅ | ❌ | ❌ |
⚙️ Complete Functional Example
<?php
// A generator function that yields numbers
function generateNumbers(): iterable {
for ($i = 1; $i <= 5; $i++) {
yield $i;
}
}
// A function that accepts any iterable
function printIterable(iterable $data) {
foreach ($data as $value) {
echo "Value: $value" . PHP_EOL;
}
}
// Using with array
printIterable([10, 20, 30]);
// Using with generator
printIterable(generateNumbers());
Output:
Value: 10
Value: 20
Value: 30
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
✅ Tips & Common Pitfalls
✔ Best Practices
-
Use
iterable
type hints when you want to support both arrays and objects. -
Combine with generators for memory-efficient looping over large data sets.
-
Always check input if it's coming from a dynamic or third-party source.
❌ Common Pitfalls
-
Modifying iterables during iteration may lead to unexpected results.
-
The
iterable
type is not backwards compatible with PHP < 7.1. -
You can’t directly apply array functions (e.g.,
array_map
) to Traversable objects.
Summary: Why Use PHP Iterables
-
Simplifies function input/output when working with loopable data
-
Reduces bugs by ensuring only valid loopable structures are used
-
Supports both traditional arrays and modern structures like generators
-
Enables cleaner, DRY code
Key Takeaways
-
Use the
iterable
type hint to accept arrays and Traversable objects. -
Great for flexible, reusable, and modern codebases.
-
Perfect with generators for lazy loading large datasets.
-
Keep your PHP version ≥ 7.1 to use it effectively.