Jump to content

Archived

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

tomfmason

Unsupported operand types

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

Share this post


Link to post
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);

Share this post


Link to post
Share on other sites
Thanks for the reply but all of the variables are numeric..  I am at a lose as to why this is happening..

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Yes I am sure.. I tested it throughly.. This is realy weird.. When I move it out side of the class to the page itself it works fine..

Share this post


Link to post
Share on other sites
so you're saying it won't divide even though both $a and $b are numeric! That is very strange. If possible please post a simple test case, inside and outside a class, that shows the problem. What version of PHP are you using?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.