在 PHP 中查找三个值中的最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1210172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:33:19  来源:igfitidea点击:

Find greatest of three values in PHP

php

提问by Gordon

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?

对于三个数字、$x$y$z,我使用以下代码找到最大的并将其放入$c. 有没有更有效的方法来做到这一点?

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

回答by Greg Hewgill

Probably the easiest way is $c = max($x, $y, $z). See the documentation on maxDocsfor more information, it compares by the integer value of each parameter but will return the original parameter value.

可能最简单的方法是$c = max($x, $y, $z)。有关更多信息,请参阅maxDocs上的文档,它通过每个参数的整数值进行比较,但会返回原始参数值。

回答by Tyler Carter

You can also use an array with max.

您还可以使用具有最大值的数组。

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

if you need to

如果你需要

回答by Ashutosh Verma

<?php
$a=20;
$b=10;
$c=1;
if($a>$b && $a>$c)
{
echo "Greater value is a=".$a;
}
else if($b>$a && $b>$c)
{
echo "Greater value is b=".$b;
}
else if($c>$a && $c>$b)
{
echo "Greater value is c=".$c;
}
else
{
echo"Dont Enter Equal Values";
}
?>

Output:

输出:

Greater value is a=20

回答by Raghuldev

Much simpler one

更简单的一个

<?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";}
} 

?>        

回答by Rahul Thakur

//Greater no. 
<?php

            $a = 15;
            $b = 68;
            $c = 60;
            if($a>$b && $a>$c)
                {
                echo "a is greater no";
                }
            elseif($b>$a && $b>$c)
                {
                echo "B is greater no";
                }            
            else
                {
                echo "C is graeter no";
                }

?>