user3358884
user3358884

Reputation: 109

PHP largest and smallest number given to a form

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

Upvotes: 0

Views: 4828

Answers (3)

Sharky
Sharky

Reputation: 6294

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

Upvotes: 0

Chitowns24
Chitowns24

Reputation: 960

Here is some logic if you want to go that route

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

Hopefully this helps

Upvotes: 1

Dushyant Joshi
Dushyant Joshi

Reputation: 3702

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

Upvotes: 1

Related Questions