PHP OOPLesson 56Free preview
PHP Iterables
This lesson is part of PHP · All levels
Summary
An iterable type is a type of hint that receives an array or object that follows a Traversable interface, such as the output of an iterator or generator. With iterable, a single function can process more than one type of data as long as all of it can be passed slowly using foreach. This type is available since PHP 7.1 and is used on parameters as well as return values.
Key points
- Iterable receives array and object Traversable (Iterator or IteratorAggregate).
- It can be used as a function/method parameter type hint and as a return type.
- The iterable type function practically receives the output of the generator without conversion.
- Iterable does not accept other data types such as strings, int, or ordinary objects.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.