samedi 16 mai 2015

Foreach loop randomly throwing “invalid argument supplied”

I'm trying to get this function to work but for some reason it errors out on the foreach line saying there is an invalid argument.

$scores= TESTAPI::GUL($user->ID);
if (empty($scores)) {
    echo "<p>No Scores</p>";
} else {
    foreach ($scores as $score) {
        echo "<p>".$score."</p>";
    }
}

The error I get is: PHP Warning: Invalid argument supplied for foreach()

Note: I need to keep the if... else... in the order it currently is.

Some variations for the if statement, prior to the for

if(!is_array($scores) && !count($scores) || empty($scores)){
if(!is_array($scores) && !count($scores)){

I can't use use var_dump($scores); because the error is on a page on a private website I run (can't provide links) and I am unable to find a way to recreate the error. Maybe someone knows a way I can capture data that causes the foreach to go wrong and save it to a log file?

Aucun commentaire:

Enregistrer un commentaire