douyanjing0822 2014-03-13 07:12
浏览 61
已采纳

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

Can any body suggest a good PHP function which help me do the smallest and largest of a user input arrays. I searched for almost an hour but I couldn't come up with any valuable function which can help me do the following assignment. Assignment: The below PHP script prints the largest and smallest number given to a form. Your task is to write the missing functions (largest and smallest), that receive the integers given by user as parameters and then return the largest and smallest integers. Incomplete program:

<?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

From the numbers you typed, the largest was 10 and smallest -1

  • 写回答

3条回答 默认 最新

  • du127953 2014-03-13 07:45
    关注

    This works for any amount of parameters

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

    edit: i made some typos, just corrected them now its ok -i added some comments also

    update: i just realized this answer is not in the form you asked for "Your task is to write the missing functions (largest and smallest), that receive the integers given by user as parameters and then return the largest and smallest integers."

    anyway i won't delete the answer i will leave it here if anybody wanna take a look since it answers the question as described in your title "PHP largest and smallest number given to a form" maybe it will be helpfull to a google visitor :D

    UPDATE 2: I decided to change the code a bit so it actually answers your question, here it is:

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

    working demo here

    http://3v4l.org/Qh0YA

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?