PHP Warning: Variable passed to each() is not an array or object

Message:

PHP Warning: Variable passed to each() is not an array or object in /home/functions.php on line 23

Error type: warning

Symptoms:

You try to use each, but instead of going through the loop, you just receive a warning.

Sample Code:

<?php
while (list($key, $val) = each ($myvar))
{
echo $key.’ ‘;
}
?>

Cause:

You try to use the each() function with a null, integer or string variable (or generally not an array or object). This might be caused by an uninitialized variable you try to use with each();

Fix:

Make sure the variable you try to pass exists and is not null first. You should also make sure you didn’t misspell the variable name (if you try to use a variable that has not been initialized you get the same warning).

You should try to use function is_array($array) to make sure the variable passed is indeed an array.

3 thoughts on “PHP Warning: Variable passed to each() is not an array or object”

  1. I take pleasure in, lead to I discovered exactly what I was taking a look for.
    You’ve ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye

Leave a Reply

Your email address will not be published.

Security question: * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.