PHP: Fix for “Invalid argument supplied for foreach()” The error is caused when the variable that foreach is attempting to iterate over is not an array. For example, let us say that I have a function called get_user_posts. This function is supposed to return an array of user comments.