Jump to content

Unsupported operand types


tomfmason

Recommended Posts

I have a class the records hits and retrieves information about users and guests online..

Well I get that error when trying to get the percentage of users/ guests online..

Here is how I call the function with in the class.

[code=php:0]
$users = new users;
$users->recordHit(session_id(), $_SERVER['REMOTE_ADDR'], $username, $_SERVER['HTTP_REFERER']);
$new_today = $users->userJoinDate('today');
$new_yesterday = $users->userJoinDate('yesterday');
$total_users = $users->totalUsers();
$new_users = $users->lastMembers();
$new_user_count = count($new_users);
$users_online = $users->usersOnline();
$count_users_online = count($users_online);
$guests_online = $users->guestsOnline();
$total_online = ($count_users_online + $guests_online);
$guest_percent = $users->percent($users_online, $total_online);
$user_percent = $users->percent($count_users_online, $total_online);
[/code]

I get the following error

Fatal error: Unsupported operand types in user.inc.php on line 83

Here is the function in question.

[code=php:0]
function percent($a, $b) {
    $percent = round(($a / $b) * 100);//line 83
    return $percent;
}
[/code]

Any suggestions as to why this is happening?

Thank you,
Tom
Link to comment
https://forums.phpfreaks.com/topic/25029-unsupported-operand-types/
Share on other sites

chances are either $a or $b is not numeric, and you can't divide or multiply values that are not numeric (though there are some exceptions)
So make sure your function receives numeric values for $a and $b.
For example, the following code will give you the same error:

$a = 8;
$b = array('ping','pong');
echo($a/$b);

OR

$a = 'ping';
$b = getdate();
echo($a/$b);
hmm,
Are you sure they are numeric?
If you can replicate the bug, the best way to find out what is happening is to print out $a and $b to truly tell what they are:
[code]
function percent($a, $b) {
    var_dump($a);
    var_dump($b);
    $percent = round(($a / $b) * 100);//line 83
    return $percent;
}
echo(percent(3,2));[/code]
This would output:
[code]int(3) int(2) 150[/code]
LOL.. No you were right.. It was not numeric..

I had

[code=php:0]
$guest_percent = $users->percent($users_online, $total_online);
[/code]

when it should have been

[code=php:0]
$guest_percent = $users->percent($guests_online, $total_online);
[/code]

$users_online is an array of usernames.

LOL thanks,
Tom

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.