ADVERTISEMENT
ADVERTISEMENT

PHP program to find the maximum and minimum element of an array

PHP | Maximum and minimum of an array: Here, we are going to implement a PHP program to find the maximum and minimum element of an array.
Submitted by IncludeHelp, on June 08, 2020

The program is to find the minimum and the maximum element of the array can be solved by two methods. This program can be practically used.

For example, a teacher has an array of marks of students and needs to declare the topper of the class. So the given program will easily give the teacher maximum marks in the array.

Program:

<?php
// Find maximum in array
function getMaxElement($array)
{
    $n = count($array);
    $max = $array[0];
    for ($i = 1;$i < $n;$i++) if ($max < $array[$i]) $max = $array[$i];
    return $max;
}

// Find maximum in array
function getMinElement($array)
{
    $n = count($array);
    $min = $array[0];
    for ($i = 1;$i < $n;$i++) if ($min > $array[$i]) $min = $array[$i];
    return $min;
}

// Main code
$array = array(
    10,
    20,
    30,
    40,
    50
);
echo (getMaxElement($array));
echo ("\n");
echo (getMinElement($array));
?>

Output:

50
10

PHP Array Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.