PHP ITERABLES

Sugar babies

PHP ITERABLES 

  • 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.
  • The iterable keyword can be used as a data type of a function argument or as the return type of a function.
  • An iterable is any value which can be looped through with a foreach() loop.

EXAMPLES FOR ITERABLES IN PHP :

  • lists
  • tuples
  • strings

SYNTAX FOR ITERABLES IN PHP :

function is_iterable($var) 
{
    return (is_array($var) || $var instanceof Traversable || $var instanceof stdClass);

}
EXAMPLE PROGRAM FOR ITERABLES IN PHP :

<?php
function printIterable ( Iterable $myIterable ){
    foreach($myIterable as $item) {
         echo $item;
     }
}
$arr = ["a", "b", "c"];
printIterable ($arr);
?>

OUTPUT :

abc
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send