PHP Basic Tutorial
MySQL Connection
PHP Advanced
An iterable is any value that can be looped through with a foreach
loop.
In PHP, an iterable can be:
Traversable
interface (like generators or iterators)The iterable
pseudo-type was introduced in PHP 7.1.
The iterable
pseudo-type can be used in function arguments and return types to indicate that the value must be something you can iterate over using foreach
.
Try it yourself
You can also return an iterable:
Try it yourself
All arrays in PHP are inherently iterable and can be passed to functions expecting an iterable
type.
Any object that implements the Iterator
interface is considered iterable. This allows for custom iteration logic.
Iterator
InterfaceTo be a valid iterator in PHP, your class must implement:
Method |
Purpose |
---|---|
current() |
Returns the value of the current item |
key() |
Returns the key of the current item (int, float, bool, or string) |
next() |
Moves the pointer to the next item |
rewind() |
Resets the pointer to the first item |
valid() |
Returns |
Try it yourself