is_iterable

is_iterable(mixed $value): bool

Checks whether a value can be traversed with foreach (array or Traversable, PHP 7.1+). Contrasts three camps: Python/JS protocol style, Go/Rust compile-time style, Java/C# interface style.

C C23

C23
#include <stdio.h>

int main(void) {
    /* C has no iterable concept: "can I loop this?" is answered by the
       type itself - arrays loop by length, everything else needs a protocol
       you invent (linked lists walk by pointer, files by fread) */
    int arr[] = {1, 2, 3};
    size_t n = sizeof arr / sizeof arr[0];
    for (size_t i = 0; i < n; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}
Output
1
2
3

C++ C++26

C++26
#include <iostream>
#include <vector>

int main() {
    // range-based for asks one compile-time question: does begin()/end()
    // exist on this type? - the "iterable check" happens before main runs
    std::vector<int> v = {1, 2, 3};
    for (int x : v) {
        std::cout << x << "\n";
    }
    return 0;
}
Output
1
2
3

C# v11.0.100

v11.0.100
using System.Collections;
using System.Collections.Generic;

var list = new List<int> { 1, 2 };
Console.WriteLine(list is IEnumerable);        // True: the interface camp
Console.WriteLine("abc" is IEnumerable<char>); // True: strings implement it too
foreach (int v in list) Console.WriteLine(v);  // foreach requires IEnumerable
Output
True
True
1
2

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"iter"
)

func main() {
	// Go 1.23's iter.Seq is the closest analog of Iterable/Traversable:
	// a function that pushes values through a yield callback
	s := iter.Seq[int](func(yield func(int) bool) {
		yield(1)
		yield(2)
	})
	for v := range s {
		fmt.Println(v)
	}
	// slices/maps/channels are ranged, not "iterable objects" - the language
	// asks "can I range over this?" at compile time, never "are you Iterable?"
}
Output
1
2

Java v27.0.1

v27.0.1
import java.util.List;

public class Main {
    static void walk(Iterable<Integer> it) {
        for (Integer v : it) {
            System.out.println(v);
        }
    }

    public static void main(String[] args) {
        walk(List.of(1, 2)); // collections implement Iterable
        System.out.println(List.of(1) instanceof Iterable); // the interface camp
        // arrays are loopable with for-each but NOT Iterable instances:
        // int[] cannot be passed to walk(Iterable<Integer>)
    }
}
Output
1
2
true

JavaScript v26.13.1

v26.13.1
console.log(typeof [][Symbol.iterator] === "function");   // true: the protocol camp
console.log(typeof "abc"[Symbol.iterator] === "function"); // true: strings too
console.log(({}).hasOwnProperty(Symbol.iterator));        // false: plain objects are not
for (const ch of "ab") console.log(ch); // for...of consumes the protocol
Output
true
true
false
a
b

PHP v8.6.0

v8.6.0
// is_iterable: the runtime check for "can foreach walk this?"
var_dump(is_iterable([1, 2, 3]));             // arrays: yes
var_dump(is_iterable(new ArrayIterator([1]))); // Traversable: yes
function gen() { yield 1; }
var_dump(is_iterable(gen()));                  // Generator: yes (Traversable)
var_dump(is_iterable('string'));               // no
var_dump(is_iterable(42));                     // no
var_dump(is_iterable(new stdClass()));         // plain objects: no (must implement Traversable)
Output
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)

Python v3.15.2

v3.15.2
from collections.abc import Iterable

print(isinstance([1, 2], Iterable))          # True: lists register the ABC
print(isinstance((x for x in [1]), Iterable)) # True: generators too
print(isinstance("abc", Iterable))           # True: strings iterate chars
print(isinstance(42, Iterable))              # False
# Python is duck typing with a formal protocol: Iterable is satisfied
# by any object that defines __iter__ - isinstance just asks for it
Output
True
True
True
False

Rust v1.99.1

v1.99.1
fn main() {
    let v = vec![1, 2, 3];
    let it = v.iter(); // anything iterable hands out an Iterator
    println!("{}", it.count());
    // Rust never asks at runtime "are you iterable?": the IntoIterator
    // trait bound is checked at compile time - wrong types fail to build
}
Output
3

TypeScript v26.13.1

v26.13.1
console.log(typeof [][Symbol.iterator] === "function");   // true: the protocol camp
console.log(typeof "abc"[Symbol.iterator] === "function"); // true: strings too
console.log(({} as Record<string, number>).hasOwnProperty(Symbol.iterator)); // false
for (const ch of "ab") console.log(ch); // for...of consumes the protocol
Output
true
true
false
a
b

Language differences

C: no iterable concept - "can I loop this?" is answered by types and conventions: arrays by length, lists by pointers, files by fread; a unified abstraction means inventing a function-pointer protocol yourself.

C++: the range-for iterability check happens at compile time - a type with begin()/end() is traversable; it is a duck-style template protocol, and at runtime the question does not exist.

C#: the interface camp - IEnumerable/IEnumerable<T> is a hard requirement for foreach, and is IEnumerable is the runtime check (example); strings, arrays and collections all implement it.

Go: the compile-time camp - slices/maps/channels/arrays are ranged by the language itself and do not form an "iterable object" concept; iter.Seq (1.23) is the closest analog of Traversable (a yield-pushing function) ranged with for.

Java: the interface camp - java.lang.Iterable is a hard requirement for for-each (everything in Collection implements it); arrays are for-each-able yet are NOT Iterable instances, a famous asymmetry of Java's traversal system.

JavaScript: the protocol camp - iterable means the object has a [Symbol.iterator] method, and for...of/spread only honor that protocol; plain objects are not iterable by default while strings are.

PHP: a runtime check for "can foreach walk it": arrays are iterable by nature, and objects must implement Traversable (Iterator or IteratorAggregate; generators satisfy it automatically); note that stdClass implements no Traversable, so is_iterable returns false.

Python: the protocol camp with ABC naming - any object defining __iter__ satisfies collections.abc.Iterable; the idiom is try-then-check (EAFP): attempt the iteration first, isinstance only on failure.

Rust: the compile-time camp - an IntoIterator trait bound makes "not iterable" a build failure, and the runtime check does not exist; the for loop is syntax sugar over it.

TypeScript: same as JavaScript; at the type level there is additionally the Iterable<T> interface annotation declaring "this is iterable" at compile time.