Home »
PHP »
PHP Programs
PHP program to find the maximum and minimum elements 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 elements of an array.
Submitted by IncludeHelp, on June 08, 2020 [Last updated : March 13, 2023]
PHP - Maximum and Minimum Elements of an Array
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.
PHP code to find the maximum and minimum elements of an array
<?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
In the above example, we used the following PHP topics:
PHP Array Programs »