Invalid argument supplied for foreach()

Description

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

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

Example


Solutions

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

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