PHP Program to Display Minimum and Maximum Value from Array

PHP Program to Display Minimum and Maximum Value from Array

  • PHP
  • 1 min read

Here is a PHP program to display the maximum value from array elements of an array.

<html>
<body>
<?php
$a=array(10,90,70,120);
$b=count($a);
$max=$a[0]; //here 10 is assigned to first array element.
for($i=0;$i<$b;$i=$i+1)
{
if($a[$i]>$max) //here each array element value will be compared with the present value of $max
{
$max=$a[$i];//here $max is the current maximum value
}
}
echo "The maximum value is=".$max;
?>
</body>
</html>

Output: The maximum value is=120

Here is a PHP program to display the minimum value from array elements of an array.

<html>
<body>
<?php
$a=array(10,90,70,120);
$b=count($a);
$min=$a[0]; //here 10 is assigned to first array element.
for($i=0;$i<$b;$i=$i+1)
{
if($a[$i]<$min) //here each array element value will be compared with the present value of $min
{
$min=$a[$i];//here $min is the current minimum value
}
}
echo "The minimum value is=".$min;
?>
</body>
</html>

Output: The minimum value is=10

See also: