min

Returns the lowest value from a list of arguments or from a single array.

Signature
min(mixed $value, mixed ...$values): mixed
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$valuemixedAny comparable value. If it is the only argument and is an array, the lowest element of that array is returned.
$valuesoptionalmixedAny number of further comparable values (variadic).

Return value

mixedThe value considered lowest by standard comparison. When several values compare equal, the first one passed is returned, with its original type unchanged.

Errors

Throws a ValueError when called with a single array that is empty.

Changelog

  • PHP 8.0.0min() throws a ValueError on failure; previously it returned false and emitted an E_WARNING.
  • PHP 8.0.0String-to-number comparison rules changed, so min() no longer returns a different result depending on argument order in those cases.

min() is the mirror of max() and shares its two call forms: several arguments, or one array whose elements are compared. Passing an array together with other arguments compares the array as a whole rather than its contents.

Comparison rules bite here

Values are ranked with PHP's standard comparison, so mixing types produces results that look wrong until you follow the rules. Comparing an int with a bool converts both to bool, which is why min(-10, false) is false — -10 becomes true, and false is lower. Comparing a number with a non-numeric string compares them as strings in PHP 8, so min('abc', 0) is 0, where PHP 7 returned 'abc' because the two were considered equal. If you mean a numeric comparison, cast first.

Practical uses

Beyond finding a smallest value, min($requested, $limit) caps a value from above without a branch — the standard way to clamp a page size or a retry delay. The return keeps its original type, and ties resolve to the first value passed.

Like max(), it returns the value rather than its key; use array_search() or array_keys() to recover the key.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Finding the cheapest plan or lowest bid in a price list.
  • Capping a user-supplied page size with min($requested, 100).
  • Getting the earliest deadline or oldest timestamp from a set of dates.
  • Limiting an exponential backoff delay to a maximum wait.

Frequently asked questions

How do I find the lowest value in a PHP array?

Pass the array as min()'s only argument: min($arr). Adding other arguments makes PHP compare the array itself against them instead of looking inside it.

What is the difference between min and sort?

min() makes one pass and returns a single value, while sort() reorders the entire array in place and returns a bool. If you only need the smallest item, min() is both faster and non-destructive.

Why does min(-10, false) return false?

Comparing an int with a bool converts both to bool, so -10 becomes true and false ranks lower. Cast explicitly when you intended a numeric comparison.

More examples

Guarding against an empty array

An empty result set is a normal outcome for a query, but min() treats it as an error and throws. The guard on the last line is the fix; a try/catch works too, but checking for empty first is cheaper and reads better than exception control flow.

Example · php
<?php
$rows = [];

try {
    echo min($rows), "\n";
} catch (ValueError $e) {
    echo "caught: ", $e->getMessage(), "\n";
}

echo "safe: ", $rows === [] ? 'n/a' : min($rows), "\n";

Earliest date, two ways

ISO 8601 dates sort correctly as plain strings, so min() on the raw strings already gives 2025-11-14 without any parsing. Converting to timestamps first is only necessary when the formats are inconsistent or you need to do arithmetic on the result.

Example · php
<?php
$deadlines = ['2026-03-01', '2025-11-14', '2026-01-09'];

echo "earliest string: ", min($deadlines), "\n";

$stamps = array_map('strtotime', $deadlines);
echo "earliest stamp:  ", min($stamps), "\n";
echo "formatted:       ", date('D d M Y', min($stamps)), "\n";

Mixed types follow standard comparison

Two arrays of the same length compare element by element, so [2,4,8] wins at the second position. '10' and 9 both look numeric, so 9 is lower and comes back as an int. The last two involve booleans, which force both sides into boolean space: 0 is false-y so it beats true, and -10 is truth-y so false beats it.

Example · php
<?php
var_dump(min([2, 4, 8], [2, 5, 1]));
var_dump(min('10', 9));
var_dump(min(0, true));
var_dump(min(-10, false));

Discussion

  • Be the first to comment on this lesson.