php8.5
Home/ Manual/ language / types/ Iterables

Iterables

Iterable is a built-in compile-time type alias for array|Traversable. From its introduction in PHP 7.1.0 and prior to PHP 8.2.0, iterable was a built-in pseudo-type that acted as the aforementioned type alias and could be used as a type declaration. An iterable type can be used in Foreach and with yield from within a generator.

Iterable is a built-in compile-time type alias for array|Traversable. From its introduction in PHP 7.1.0 and prior to PHP 8.2.0, iterable was a built-in pseudo-type that acted as the aforementioned type alias and could be used as a type declaration. An iterable type can be used in Foreach and with yield from within a generator.

Note

Functions declaring iterable as a return type may also be generators.

Iterable generator return type example

php
<?php

function gen(): iterable {
    yield 1;
    yield 2;
    yield 3;
}

foreach(gen() as $value) {
    echo $value, "\n";
}
?>

Source: language/types/iterable.xml · from the official PHP manual (php/doc-en)