Warning: Invalid Argument Supplied For foreach()

Today while refreshing my programming knowledge (those I left once completed my college), I repeatedly encountered an error message "Warning: Invalid argument supplied for foreach()". I was trying to display the items received from a checkbox form on browser using the foreach() loop. I created one HTML form with facility to receive multiple inputs from users using "checkbox" and  pass them to action page using the method "POST". As we know, the multiple inputs create an  array which can be accessed using $_POST["name given to checkbox"] and displayed using foreach loop. However, instead of displaying the user inputs, I received the error message " Warning: Invalid argument supplied for foreach() ". You can see the browser response from the picture attached below.



Reason:
foreach() works for arrays and if the passed variable is not a valid array, we will get the "Invalid argument' warning. To avoid this warning and makes the program run, we need to make sure the HTML form is passing a valid array to the PHP script on which the foreach loop runs.

Now, let us tweak the code. This part of the PHP code is invoking the above error.
<?php
if (!empty($_POST["OS"]))
{
foreach ($_POST["OS"] as $val)
{
print "$val"."<br/>";
}
}
?>

This code is logically correct, so I decided to check the HTML portion.


(Wrong Code)
<form action="action1.php" method="post">
<input type="checkbox" name="OS" value="Linux" /><br/>
<input type="checkbox" name="OS" value="Windows" /><br />
<input type="checkbox" name="OS" value="Android" /><br />
<input type="submit" value="Submit" />
</form>

My logic was correct except the failure to use an array to store the inputs from users. Once I changed the name of the checkbox parameters to OS[], the warning disappeared. The updated code is given below.

<form action="action1.php" method="post">
<input type="checkbox" name="OS[]" value="Linux" /><br/>
<input type="checkbox" name="OS[]" value="Windows" /><br />
<input type="checkbox" name="OS[]" value="Android" /><br />
<input type="submit" value="Submit" />
</form>


Tips to Follow When You See This Warning

There might be various reasons which can bring Warning: Invalid argument supplied for foreach() and it is better you learn different approaches to solve it. If you get the same warning, you may simply follow the steps mentioned below.

  1. Make sure it is an array

    As we know foreach() works with array, it is important to check the variable before executing it. You can use the following code to check whether the variable is a valid array.
    if (is_array($_POST["OS"]))
    {
         foreach ($_POST["OS"] as $val)
    }

    Here if the variable is not an array, it won't execute.

  2. Use an Array to Store values in HTML form

    This is the mistake I made at first. Instead of using an array, I used a simple variable to store values.

  3. Use of (array)

    By placing (array) before the variable, we can stop seeing the message from PHP. See the example below.

    foreach ((array)$_POST["OS"] as $val)
    {
    ---- Code------
    }

Related Tutorials
  1. Redirect Links Pointing to Old Blogger Posts to New Posts

  2. Can I Remove Date From Blogger URL

  3. display The Blog Posts in Reverse Order