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;
}
If you want to Compare Three Variables. Compare two integers and get maximum of them by using max() function. Then compare the maximum with the third variable! Also you could do it just in one line max(max($x, $y), $z) .
The max() function of PHP is used to find the numerically maximum value in an array or the numerically maximum value of several specified values. The max() function can take an array or several numbers as an argument and return the numerically maximum value among the passed parameters.
The factorial of a number n is defined by the product of all the digits from 1 to n (including 1 and n). For example, 4! = 4*3*2*1 = 24.
Probably the easiest way is $c = max($x, $y, $z)
. See the documentation on max
Docs for more information, it compares by the integer value of each parameter but will return the original parameter value.
You can also use an array with max.
max(array($a, $b, $c));
if you need to
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With