How To Use Iterables Function in PHP Example

PHP 8 , PHP

Nicesnippets

424

30-07-2021


How To Use  Iterables Function in PHP Example

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.

I hope it can help you...


Recommended Posts