Find greatest of three values in PHP
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;
}
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
更简单的一个
<?php
$one = 10;
$two = 20;
$three = 30;
if ($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";}
}
?>
链接地址: http://www.djcxy.com/p/58510.html
上一篇: PHP字符串数字连接搞砸了
下一篇: 在PHP中查找最大的三个值