PHP赋予表单的最大和最小数字

时间:2014-03-13 07:12:03

标签: php

任何正文都可以建议一个好的PHP函数,它可以帮助我完成最小和最大的用户输入数组。我搜索了差不多一个小时,但我无法想出任何有助于我完成以下任务的有价值的功能。 的分配: 下面的PHP脚本打印给表单的最大和最小数字。您的任务是编写缺少的函数(最大和最小),它们接收用户给出的整数作为参数,然后返回最大和最小的整数。不完整的计划:

<?php
    $first = $_GET['first'];
    $second = $_GET['second'];
    $third = $_GET['third'];         

    //Your code here

    $largest_number = largest($first, $second, $third);
    $smallest_number = smallest($first, $second, $third);    

    echo "From the numbers you typed, the largest was $largest_number";
    echo " and smallest $smallest_number";    
?>

Example output

  

根据您输入的数字,最大值为10,最小值为-1

3 个答案:

答案 0 :(得分:1)

如果你想走这条路线,这是一些逻辑

<?php
$first = $_GET['first'];
$second = $_GET['second'];
$third = $_GET['third'];  
$numbers = array($first, $second, $third); 
 function largest($array)
{
   $largest = $array[0];
   foreach($array as $value)
   {

    if($value > $largest)
    {

    $largest = $value;
    }

  }

    return $largest;    
}
function smallest($array)
{
   $smallest = $array[0];
   foreach($array as $value)
   {

    if($value < $smallest)
    {

    $smallest = $value;
    }

  }

    return $smallest;   
}      

$largest_number = largest($numbers);
$smallest_number = smallest($numbers);    

echo "From the numbers you typed, the largest was $largest_number";
echo " and smallest $smallest_number";    
?>

希望这会有所帮助

答案 1 :(得分:0)

$smallest = min ( array(2, 4, 5)  )// prints 2
$largest = max ( array(2, 4, 5)  )// prints 5

答案 2 :(得分:0)

这适用于任何数量的参数

<?php

foreach($_GET as $a_parameter)
{

if($a_parameter==intval($a_parameter))   // only if parameter is a number
{
    if(!isset($minimum)) // for the first time paremeter is minimum
    {
        $minimum = $a_parameter;
    }

    if(!isset($maximum)) // for the first time paremeter is maximum
    {
        $maximum= $a_parameter;
    }

    if($a_parameter<$minimum)  // for each parameter we check if its lower and set minimum
    {
        $minimum=$a_parameter;
    }

    if($a_parameter>$maximum) // for each parameter we check if its greater and set maximum
    {
        $maximum=$a_parameter;
    }


}

echo "From the numbers you typed, the largest was ".$maximum." and smallest ".$minimum;

?>

编辑:我做了一些拼写错误,现在纠正了它们的确定-i还添加了一些评论

更新:我刚刚意识到这个答案不是你要求的形式&#34; 你的任务是编写接收整数的缺失函数(最大和最小)由user作为参数给出,然后返回最大和最小的整数。&#34;

无论如何我不会删除答案我会留在这里,如果有人想看一看,因为它回答了你的标题中所描述的问题&#34; PHP给表格的最大和最小数字& #34; 也许对谷歌访问者有帮助:D

更新2:我决定更改一下代码,以便它实际回答您的问题,现在是:

<?php
$first = 34; //$_GET['first'];
$second = -55; //$_GET['second'];
$third = 90; //$_GET['third'];         

//Your code here




function smallest($first, $second, $third)
{
    foreach(func_get_args() as $a_parameter)
    {

        if($a_parameter==intval($a_parameter))   // only if parameter is a number
        {

            if(!isset($minimum)) // for the first time paremeter is maximum
            {
                $minimum= $a_parameter;
            }

            if($a_parameter<$minimum)  // for each parameter we check if its lower and set minimum
            {
                $minimum=$a_parameter;
            }
        }

    }
    return $minimum;
}


function largest($first, $second, $third)
{
    foreach(func_get_args() as $a_parameter)
    {

        if($a_parameter==intval($a_parameter))   // only if parameter is a number
        {
            if(!isset($maximum)) // for the first time paremeter is maximum
            {
                $maximum= $a_parameter;
            }

            if($a_parameter>$maximum) // for each parameter we check if its greater and set maximum
            {
                $maximum=$a_parameter;
            }
        }
    }
    return $maximum;
}


$largest_number = largest($first, $second, $third);
$smallest_number = smallest($first, $second, $third); 

echo "From the numbers you typed, the largest was $largest_number";
echo " and smallest $smallest_number";    

?>

在这里工作演示

http://3v4l.org/Qh0YA