Skip to main content

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 the browser using the foreach() loop. I created one HTML form with the 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 the 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.

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.
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" />

My logic was correct except for 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" />

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 an 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


About Author

My photo
Alex George
Alex George has Engineering graduation in Computer Science and an MBA in Finance. He had been working as a senior Network Engineer for ten years. His specialization is in Cisco products. Traveling is one of his hobbies and visited various places as part of his onsite work. He visited different places as part of his onsite work like New York, Dubai, London, etc. Right now Alex is a director of one of the leading career development firms in India, which trains students for UPSC, SSC, and PSC examinations. He guides a lot of students to get selection for various prestigious institutions like CBI, Income Tax Department, etc. Alex George is a stock market investor and a very active intraday trader. Engineering Background: He has a B.Tech in Computer Science and Engineering and passed different network and security courses like CCNA, CCNP (Security), CEH, and various Microsoft certifications. Finance Background: Alex has an MBA in Finance. He is an active intraday trader and a Share Market Investor. Webmaster Skills: Alex is a blogger since 2004. He has a working knowledge of HTML, CSS, PHP, and JavaScrip.