Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Invalid argument supplied for foreach()

Description

foreach() works on arrays or objects. For the array, all the elements of the array are read; for the objects, it is either the public properties, read like an array, or the Iterator interface, which uses specific methods.

Then, any other type is forbidden to be used with foreach(). This means all scalars elements, including string, integer and null.

Example

<?php

foreach(1234 as $b) {
	echo $b;
}

?>

Alternatives

  • Check that the source of the foreach can be used with is_iterable().

In more recent PHP versions, this error message is now :ref:foreach()-argument-must-be-of-type-array|object.