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()
$scores = TESTAPI::GUL($user->ID);
if (is_array($scores) && count($scores)) {
foreach ($scores as $score) {
echo "<p>".$score."</p>";
}
} else {
echo "<p>No Scores</p>";
}
Looks like $scores
is neither an array nor an object...
For example, empty('')
would also be true
.
I would recommend to check is_array($scores) && count($scores)
instead of empty()
, to make sure the api returned useable output (an array) and that this contains elements (count() > 0
which is true).
Try this -
foreach ((array) $scores as $score) { ...