CKTIS / PHP
is_iterable()
Identify arrays and objects implementing Traversable before passing data to an iterable parameter.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
is_iterable(mixed $value): boolIdentify arrays and objects implementing Traversable before passing data to an iterable parameter. A generator qualifies even without random access. A plain object can expose public properties to foreach but does not satisfy the iterable type.
Signature
is_iterable(mixed $value): bool
Example output
[true,true,false,false]
Examples
<?php
function one(): Generator { yield 1; }
$plain = (object)["label"=>"x"];
echo json_encode([is_iterable([]), is_iterable(one()), is_iterable($plain), is_iterable("x")]);
Result
[true,true,false,false]