Register Login

PHP Warning: Invalid argument supplied for foreach()

Updated Apr 03, 2020

At some point in the life of a PHP developer, he will come across the error “Warning: Invalid argument supplied for foreach()". This error is encountered when you are trying to pass an invalid argument for the foreach() function, that it cannot understand.

In this article, we will try to understand the root cause of this error and the ways to prevent it.

What is "Warning: Invalid Argument Supplied for foreach()" Error?

This error usually occurs when you are trying to pass a string value to the PHP built-in function foreach(). As the foreach() method is used for iterating over an array of elements, it expects an array from the user. When it receives anything other than an array, such as a string if throws the error.

Example 1

<?php
$mystring = 'Stechies';
foreach($mystring as $ms){
echo 'Array Value: '.$ms;
}
?>

Output

Warning: Invalid argument supplied for foreach() in \foreach.php on line 4

Correct Code

<?php
$mystring = array('S','T','E','C','H','I','E','S');
foreach($mystring as $ms){
echo ' '.$ms;
}
?>

Output

S T E C H I E S

Explanation

In the program given above, you can see that the foreach() method is passed the argument $mystring, which is an array. $mystring is converted into an array using the in-built array() method. So, the loop iterates over the array and prints out the elements with space in between them, as mentioned in the code.  

Example 2: Program to Check If an Object Is an Array or Not

<?php
$mystring = array('S','T','E','C','H','I','E','S');
if(is_array($mystring) || is_object($mystring)){
foreach($mystring as $ms){
echo ' '.$ms;
}
}
?>

Output

S T E C H I E S

Explanation

Here, you can see that the array stored in the variable $mystring is evaluated to see whether it is an array or an object. Inside the if statement two methods are used, is_array() and is_object(). The is_array() method is used for checking if a variable is an array or not. The is_object() function is used for checking if a variable is an object or not.

The if statement checks whether $mystring is an object or an array. As there is an OR in between the statements, if either one of the conditions are True, the foreach() method iterates over the array elements and displays them using the echo statement. The result shows that the characters have a space in between.    

Example 3: Force Conversion of a String to Array

<?php
$mystring = 'STECHIES';
$mystring = is_array($mystring) ? $mystring : array();
foreach($mystring as $ms){
echo ' '.$ms;
}
?>

Output

STECHIES

Explanation

The code here demonstrates how a string can be converted into an array. You can observe that the is_array() method accepts the $mystring variable as the argument. The conditional statement in the second line checks if the variable is an array or not. If it is false, the $mystring is converted into an array using the array() method.

Then the foreach() method is used for iterating over the elements of the array and printing them out using the echo statement.   


×