Page 1 of 1

Basic Maths Equation

Posted: Tue Apr 25, 2006 10:05 pm
by furiousweebee
This is going to sound stupid, but I've never done any maths equations in PHP before so I thought I'd probably get a fairly fast answer here.

What I have is two values coming from a database which represent a home team's goals, and away team's goals, in a soccer game (eg 4 and 5). I want to display on the page who the winner was, so basically, I want to subtract the second value from the first one, and if the value is positive, set the result to "home". If it's even (i.e. the two values were the same), set the result to "draw", and if the value is negative, set the result to "away". Here's what I've got:

Code: Select all

$difference = $current_page['home_team_goals'] - $current_page['away_team_goals'];
if ($difference >= 1) { $result= "home"; }
elseif ($difference <= -1) { $result= "draw"; }
else { $result= "away"; }
I know it's probably simple as hell but I'm not sure if there's a much easier way or whatever. Any help would be greatly appreciated.

Re: Basic Maths Equation

Posted: Tue Apr 25, 2006 10:24 pm
by Christopher
You don't need the calculation and it is probably better and clearer to just check directly.

Code: Select all

if ($current_page['home_team_goals'] > $current_page['away_team_goals']) {
    $result= "home";
} elseif ($current_page['home_team_goals'] < $current_page['away_team_goals']) {
    $result= "away"; 
} else {
    $result= "draw"; 
}

Posted: Tue Apr 25, 2006 10:37 pm
by furiousweebee
Man, I swear to God I am the most stupid person alive sometimes!

Thanks so much.