Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question about how to find the value with the largest number 1

Status
Not open for further replies.

natefanaro

Technical User
Jun 10, 2002
17
US
I am trying to find out what value has the largest number, then display the appropriate result from a database. $a, $b, and $c all have a number value. (1-5 so far) and whatever is the largest value, it will show the corresponding value ($a shows $num1, $b shows $num2, $c shows $num3). I am able to get it to show the right answer as long as there is not a tie (ie $a = 2 and $b = 2) by using the bottom script. Any ideas on how to A) get the thing working where if there is a tie (ie $a = 2 and $b = 2), it will just show the value for $a, and B) maybe shorten up this code a bit? Thanks in advance.

if ($a > $b ) {
if ($a > $c ) {
echo "$num1";
}
}

if ($b > $a) {
if ($b > $c) {
echo "$num2";
}
}

if ($c > $a) {
if ($c > $b) {
echo "$num3";
}
}
 
This should work:
Code:
if (($a > $b && $a > $c) || ($a == $b || $a == $b))
    echo $num1; //Display if $a is greater than b and c, or if $a is equal to b or c
else if (($b > $a && $b > $c) || ($b == $c))
    echo $num2; //Display if $b is greater than a and c, or if b is equal to c (no need to compare against a, as this would be handled by the first if
else if ($c > $a && $c > $b)
    echo $num3; //Display if c is greater than a and b, it can't be equal to the others, as this would have already been handled.
else
    echo "Error.";
//Daniel
 
or:

max(intval($a),intval($b),intval($c)); ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Daniel, you rock. Works great. KarveR, you were a tad too late, I tried Dan's before you posted, but thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top