divide and 0

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

divide and 0

Post by blacksnday »

Im trying to create a rating system, and to tally the
current rating I am dividing total score by total number of votes.

If there is 0 votes (which in turn means 0 score)
it throws error of course, since dividing by zero doesnt make since.

How can I correct the below code so that if there is 0 it wont throw errors

Code: Select all

$rating = $tvotes / $nvote;
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

guess i could use something like

Code: Select all

if ($nvote == 0){
	}else{
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Yes. Use an if statement. Do not divide if there is nothing to divide by.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Or...

Code: Select all

$rating = $tvotes / max(1, $nvotes);
This way it will always be 1 or higher for the divisor.

The MIN and MAX commands are little used in PHP and they can be used to replace alot of IF/ELSE statements.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

That will give the vote a score equal to that of the total votes.. in other words, it'll give it 100%.

The if ($votes == 0) is a better solution.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Code: Select all

$rating = ($nvote) ? $tvotes / $nvote : 0;
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

sweatje wrote:

Code: Select all

$rating = ($nvote) ? $tvotes / $nvote : 0;
thanks a ton! that works without needing IF statements.

I really needed solution without needing IF statements because
how it is currently being done, coding was doubled....

damn.. one day my mind will soak up classes and OOP
and all this extra coding will cease lol :P
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

That is effectively and IF statement, just in a short 'version' called a ternary operator :)
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

blacksnday wrote:
sweatje wrote:

Code: Select all

$rating = ($nvote) ? $tvotes / $nvote : 0;
thanks a ton! that works without needing IF statements.

I really needed solution without needing IF statements because
how it is currently being done, coding was doubled....

damn.. one day my mind will soak up classes and OOP
and all this extra coding will cease lol :P
No OOP is site there:

http://www.php.net/manual/en/language.o ... on.ternary
Post Reply