tags:

views:

230

answers:

3

With three numbers, $x, $y, and $z, I use the following code to find the greatest and place it in $c. Is there a more efficient way to do this?

$a = $x;
$b = $y;
$c = $z;
if ($x > $z && $y <= $x) {
    $c = $x;
    $a = $z;
} elseif ($y > $z) {
    $c = $y;
    $b = $z;
}
+15  A: 

Probably the easiest way is max($x, $y, $z). See the documentation on max for more information.

Greg Hewgill
+7  A: 

You can also use an array with max.

max(array($a, $b, $c));

if you need to

Chacha102
A: 

Much simpler one

$two) { if($one>$three) {echo "one is the greatest";} else {echo "three is the greatest";} } else { if ($two>$three) {echo "two is the greatest";}

else {echo "three is the greatest";} }

?>

Raghuldev
Retyping the original code with poor formatting does not make a good answer.
Gordon