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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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