Basic Maths Equation

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
furiousweebee
Forum Commoner
Posts: 69
Joined: Sun Jul 11, 2004 7:38 am
Location: Brisbane, Australia
Contact:

Basic Maths Equation

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Basic Maths Equation

Post 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"; 
}
(#10850)
furiousweebee
Forum Commoner
Posts: 69
Joined: Sun Jul 11, 2004 7:38 am
Location: Brisbane, Australia
Contact:

Post by furiousweebee »

Man, I swear to God I am the most stupid person alive sometimes!

Thanks so much.
Post Reply