tomfmason Posted October 25, 2006 Share Posted October 25, 2006 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 errorFatal error: Unsupported operand types in user.inc.php on line 83Here 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 Quote Link to comment Share on other sites More sharing options...
BrianPeiris Posted October 25, 2006 Share Posted October 25, 2006 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); Quote Link to comment Share on other sites More sharing options...
tomfmason Posted October 25, 2006 Author Share Posted October 25, 2006 Thanks for the reply but all of the variables are numeric.. I am at a lose as to why this is happening.. Quote Link to comment Share on other sites More sharing options...
BrianPeiris Posted October 25, 2006 Share Posted October 25, 2006 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] Quote Link to comment Share on other sites More sharing options...
tomfmason Posted October 25, 2006 Author Share Posted October 25, 2006 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.. Quote Link to comment Share on other sites More sharing options...
BrianPeiris Posted October 25, 2006 Share Posted October 25, 2006 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? Quote Link to comment Share on other sites More sharing options...
tomfmason Posted October 25, 2006 Author Share Posted October 25, 2006 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 Quote Link to comment Share on other sites More sharing options...
BrianPeiris Posted October 25, 2006 Share Posted October 25, 2006 :) no problem, glad I could help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.