Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
PHP Iterables
Definition and Usage
From version 7.1 onwards, PHP provides a new pseudo-type called iterable. Any object (such as array) that implements Traversable interface is acceepted by it. This type uses foreach construct or a generator function that yields one value at a a time.
Syntax
A function can have iterable as type of its parameter to let the function accept a set of values used in foreach statement. If the parameter doesn't support foreach loop, PHP parser throws TypeError
Example
"; } } myfunc($arr1); ?>
Output
This will produce following result −
PHP Java Python
A PHP function can also return an iterable data type such as array. We use is_iterable() function to verify type of returned value.
Example
Output
This will produce following result −
bool(true)
Following is an example of generator with iterable return type
Example
PHP Version
Iterable pseudo-type was introduced in PHP 7.1
