PHP get the minimum number in an array
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 ๐
More PHP tutorials
See allPHP
PHP form validation basic example
Dec 22, 2022
PHP
PHP validate email address (with code examples)
Dec 22, 2022
PHP
How to redirect using PHP with code examples
Dec 22, 2022
PHP
Handling form data using PHP (GET and POST)
Dec 21, 2022
PHP
How to fix Wordpress PHP error missing MySQL extension
Dec 21, 2022