Skip to content

All topics  /  PHP

How To Use Iterables Function in PHP Example

PHP ·

Hi guys,

Today i will explained What is an Iterable In PHP. This example is so easy to use in php.

An iterable is any value which can be looped through with a foreach() loop. The iterable pseudo-type was introduced in PHP 7.1, and it can be used as a data type for function arguments and function return values.

So let's start to the example.

Iterable


The iterable keyword can be used as a data type of a function argument or as the return type of a function.

<!DOCTYPE html>
<html>
<body>
<?php
    function printIterable(iterable $myIterable) {
      foreach($myIterable as $item) {
        echo $item;
      }
    }
    $arr = ["a", "b", "c"];
    printIterable($arr);
?>
</body>
</html>

Output

abc

Now you can check your own.