…». When developing your code in PHP, the programmer may encounter the error message “ Invalid argument supplied for foreach in ...”. After this message is usually followed by an indication of its specifics, for example, "/ modules/tasks/todo_tasks_sub.php on line 121". The error is usually due to the specifics of the available code segment, and requires verification of the peculiarities of using variables in it. Let's look at the error factors and how to fix it.
The content of the article:
- Причины появления Invalid argument supplied for foreach 1 Causes of Invalid argument for foreach
- Как исправить ошибку «Invalid argument supplied for foreach in» 2 How to fix the error "Invalid argument for foreach in"
- Ошибка в WordPress 3 Error in WordPress
- Заключение 4 Conclusion
Causes of Invalid argument supplied for foreach
The error in question usually arises in a situation where the variable that foreach tries to execute (repeat) is not an array. For example, you pass into a loop not a array, but a scalar, or you use a double array, and forget to determine how the index is selected.
Let's assume that we have a function called get_user_posts. This function should return an array of user comments. However, if there are no comments, the function returns the logical value FALSE.
![PHP code sample](../../files/2019/02/function-php.jpg)
In the code above, we assumed that the $ posts variable would always be an array. However, if the get_user_posts function returns a logical value of FALSE, then the foreach loop will not work, and PHP will output the following error message:
Warning: Invalid argument for foreach () on line 7
How to solve this problem? Let's figure it out.
This is interesting: installing PHP 7.0.5 on Debian.
How to fix the "Invalid argument for foreach in" error
The solution depends on what your code is for. That is, if the get_user_posts function should always return an array, then obviously you need to find out why it returns a logical value of FALSE or a NULL value. The reason for this may be several things:
- Failed to declare empty array “default” (default);
- Failed to query the database;
- The array is overwritten or reset. This often happens in scripts with a large number of arrays, when there are memory constraints, and the developer is forced to drop arrays with which he or she has finished working.
Looking through someone else’s code, we may encounter APIs and functions that return FALSE when no results are found. If so, then you can add the following check to your code:
![Check php code Проверка php-кода](../../files/2019/02/function-php1.jpg)
Above, we use the is_array function to check if $ posts is an array. And we do this BEFORE trying to loop it using the foreach construct. As we already wrote, it all depends on what the purpose of your script. Adding the is_array check is unwise in a situation where there are questions about whether a variable will be an array. After all, you will hide an error, which should not exist.
WordPress Error
Also, the considered error “Invalid argument supplied for foreach in” can appear during the work of sites on WordPress. The problem is caused by the fact that WP_Block_Parser performs several string manipulations with substr () and strlen (), assuming that they work with single bytes, and not with multibyte sequences.
Resolving the error Invalid argument supplied for foreach in WordPress helps to change the value of the setting mbstring.func_overload to 0 (usually costs 2). Save the changes made, and try to go to the previously problematic page.
, и скопировать в аналогичную папку на вашем хостинге. If this does not help, try downloading WordPress 4.9.5, extract the wp-includes folder from it, and copy it to a similar folder on your hosting. After this, WordPress can offer to update your databases, agree, and it will work after clearing the cache.
Conclusion
The error "Invalid argument supplied for foreach in ..." in PHP code is usually caused by a variable that is not an array. Last tries to execute foreach, but unsuccessfully. To solve the problem, you can add the function is_array (it will check if the variable is an array). You can also recommend a general fact-finding material on the phpfaq.ru website, where you can find out in detail how to find the error in the code you created.