To get the minimum or lowest number in a PHP array, you need to call the min()
function.
The min()
function in PHP is used to get the lowest value in an array or in several specified values.
The syntax is as follows:
min($array);
// or
min($value1, $value2, $value3, ...);
The return value depends on the parameters you passed to the function.
When you pass an array of numbers, this function returns the minimum number:
// ๐ compare using array or values
echo min([17, 2, 3, 20, 5]); // 2
echo min(24, 7, 19); // 7
// ๐ compare int and float numbers
echo min(24.9, 7.3, 19.2); // 7.3
While the function works with other types like string
and boolean
, itโs not recommended because the comparison can produce unpredictable results.
Consider the following examples:
// ๐ compare string and int
echo min('hello', 0); // 0
// ๐ compare string and boolean
echo min('false', true); // false (string)
// ๐ compare boolean and int
echo min(true, 29); // 1
Comparing non-number values using the min()
function is kind of nonsense, so it shouldnโt happen if you code your application right.
And thatโs how you find the minimum number in PHP ๐