代码之家  ›  专栏  ›  技术社区  ›  Gordon

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

php
  •  14
  • Gordon  · 技术社区  · 15 年前

    有三个数字, $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;
    }
    
    5 回复  |  直到 6 年前
        1
  •  25
  •   hakre    12 年前

    可能最简单的方法是 $c = max($x, $y, $z) . 请参阅上的文档 max Docs 有关详细信息,它按每个参数的整数值进行比较,但将返回原始参数值。

        2
  •  12
  •   hakre    12 年前

    也可以使用具有max的数组。

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

    如果你需要

        3
  •  0
  •   Grant Miller    7 年前
    <?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";
    }
    ?>
    

    输出:

    Greater value is a=20
    
        4
  •  -2
  •   Gordon    11 年前

    简单多了

    <?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";}
    } 
    
    ?>        
    
        5
  •  -3
  •   R. Barzell    8 年前
    //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";
                    }
    
    ?>