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 ๐Ÿ‘

Take your skills to the next level โšก๏ธ

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.