Jump to content


Photo

Unsupported operand types


  • Please log in to reply
7 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 25 October 2006 - 08:17 AM

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.

$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);

I get the following error

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

Here is the function in question.

function percent($a, $b) {
    $percent = round(($a / $b) * 100);//line 83
    return $percent;
}

Any suggestions as to why this is happening?

Thank you,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 BrianPeiris

BrianPeiris
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 October 2006 - 08:39 AM

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);

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 25 October 2006 - 08:51 AM

Thanks for the reply but all of the variables are numeric..  I am at a lose as to why this is happening..

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 BrianPeiris

BrianPeiris
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 October 2006 - 08:59 AM

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:
function percent($a, $b) {
    var_dump($a);
    var_dump($b);
    $percent = round(($a / $b) * 100);//line 83
    return $percent;
}
echo(percent(3,2));
This would output:
int(3) int(2) 150


#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 25 October 2006 - 09:09 AM

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..

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 BrianPeiris

BrianPeiris
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 October 2006 - 09:12 AM

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?

#7 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 25 October 2006 - 09:13 AM

LOL.. No you were right.. It was not numeric..

I had

$guest_percent = $users->percent($users_online, $total_online);

when it should have been

$guest_percent = $users->percent($guests_online, $total_online);

$users_online is an array of usernames.

LOL thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#8 BrianPeiris

BrianPeiris
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 October 2006 - 09:16 AM

:) no problem, glad I could help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users