Last Updated:

Minimum and maximum : PHP

min

 

This function returns the smallest of the numbers specified in its arguments.
Syntax:

mixed min (mixed $arg 1 [int $arg 2, ..., int $argn])


There are two ways to call this function: with one parameter or with several. If only one parameter (the first) is specified, then it must be an array and the minimum element of this array is returned. Otherwise, the first (and other) arguments are treated as floating-point numbers, they are compared, and the smallest one is returned.

 

The type of return value is chosen as follows: if at least one of the numbers passed to the input is specified in floating-point format, then the result will be floating-point, otherwise the result will be an integer. With this function, you cannot lexographically compare strings— only numbers.

 


$x = min (5,3,4,6,5,6,8,9);
// $x = 3
$x[0]=4;
$x[1]=1;
$x[2]=5;
$x[3]=2;
echo min ($x); output 1


max

 

Get the largest argument.


Syntax:


mixed max (mixed $arg 1 [int $arg 2, ..., int $argn])
Function works similarly to min (), only it looks for the maximum value.
$x = max (5,3,4,6,5,6,8,9);
// $x = 9
$x[0]=4;
$x[1]=1;
$x[2]=5;
$x[3]=2;
echo max ($x); output 5