Fixing the "invalid argument supplied for foreach()" PHP error

The "invalid argument supplied for foreach()" error​ occurs when PHP’s built-in foreach() tries to iterate over a data structure that is not recognized as an array or object.

Consider the code snippet below:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

The error occurred because the getList() function returned a boolean value instead of an array. The foreach() command can only iterate over arrays or objects.

To resolve this error, perform a check before the foreach() function so that the error can be circumvented. This solution is shown in the code below:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved