Iterables in PHP represent a concept that was introduced to generalize the way collections of data can be traversed. Prior to the introduction of iterables, we primarily used arrays or instances of Traversable (like Generator or objects that implement Iterator) to represent collections of items that you could loop over.
An iterable is essentially a type hint that can accept any value which can be looped over using a foreach loop. This includes arrays and objects that implement the Traversable interface.
Starting with PHP 7.1, you can type-hint a function or method to accept an iterable:
function printIterable(iterable $myIterable) {
foreach ($myIterable as $item) {
echo $item . PHP_EOL;
}
}
printIterable([1, 2, 3, 4]); // Works with arrays
printIterable(new ArrayIterator([5, 6, 7, 8])); // Works with Traversable objects
You can also declare that a function or method should return an iterable:
function getIterable(): iterable {
return ["apple", "banana", "cherry"];
}
foreach (getIterable() as $fruit) {
echo $fruit . PHP_EOL;
}
Generators are a form of iterable that allow you to generate values on the fly without having to create an array in memory.
function generatorFunction(): iterable {
yield 'a';
yield 'b';
yield 'c';
}
foreach (generatorFunction() as $letter) {
echo $letter . PHP_EOL;
}
Another way to create custom iterables is by having your class implement the Iterator interface.
class MyIterableClass implements Iterator {
private $items = ["apple", "banana", "cherry"];
private $index = 0;
public function current() {
return $this->items[$this->index];
}
public function next() {
$this->index++;
}
public function key() {
return $this->index;
}
public function valid() {
return isset($this->items[$this->index]);
}
public function rewind() {
$this->index = 0;
}
}
$myIterable = new MyIterableClass();
foreach ($myIterable as $fruit) {
echo $fruit . PHP_EOL;
}
PHP 8 introduced support for generics via the @template annotation in DocBlocks. While PHP itself doesn't have built-in generics support, static analysis tools like Psalm and PHPStan recognize this.
This allows for more specific iterable type hints:
/**
* @template T
* @param iterable<T> $input
* @return T|null
*/
function first(iterable $input) {
foreach ($input as $item) {
return $item;
}
return null;
}
echo first([1, 2, 3]); // Outputs 1
While this doesn't change runtime behavior, tools can detect type issues at development time.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here