foreach() argument must be of type array|object

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

<?php

$variable = null;
foreach($variable as $x) {

}
?>

Solutions

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

In previous PHP versions, this error message used to be Invalid argument supplied for foreach().