W3docs

min()

Today, we will discuss the min() function in PHP. This function is used to find the lowest value in an array or a set of values.

The min() function in PHP returns the lowest value from a list of arguments or from a single array. It is a built-in math function, so you never need to write your own loop to scan for the smallest item. This page covers both call styles, how min() compares different data types, and the edge cases that trip people up.

Syntax

min() accepts two distinct call signatures:

// 1. A single array — returns its lowest element
min(array $values): mixed

// 2. Two or more separate arguments — returns the lowest of them
min(mixed $value1, mixed $value2, mixed ...$values): mixed

In both cases the return value is the actual element that was lowest (with its original type), not a copy or an index.

Passing a single array

The most common use is handing min() one array and letting it find the smallest element:

php— editable, runs on the server

min() walks the whole array and returns the smallest element — here 1.

Passing several arguments

You can also pass values directly as separate arguments. This is handy when the numbers are not already collected in an array:

<?php
echo min(2, 3, 1, 6, 7); // 1
?>

Both forms produce the same result; pick whichever fits the data you already have.

Comparing strings

min() is not limited to numbers. With strings it uses PHP's standard comparison rules, so plain words sort alphabetically:

<?php
echo min("apple", "banana", "cherry"); // apple
?>

"apple" is returned because it comes first alphabetically.

How mixed types are compared

When the values are of different types, min() relies on PHP's comparison operators. Since PHP 8, a number compared with a non-numeric string is compared as a string, which changed some older results:

<?php
var_dump(min(0, "hello")); // int(0)  (PHP 8+)
?>

If you mix types on purpose, be explicit about what you expect — comparisons across types are a common source of subtle bugs. When in doubt, normalize your data to one type first.

Negative numbers and floats

min() handles negative numbers and floats exactly as you would expect, and it preserves the type of the value it returns:

<?php
echo min(-10, 0, 10);          // -10
var_dump(min(1.5, 2, 3));      // float(1.5) — the return keeps its float type
?>

Comparing arrays

If every argument is itself an array, min() compares them: arrays with fewer elements are considered smaller, and arrays of equal length are compared element by element from left to right:

<?php
// Same length, so compared element by element: 4 < 5 at index 1
var_dump(min([2, 4, 8], [2, 5, 1]));
// Returns: [2, 4, 8]
?>

Gotcha: empty arrays

Calling min() on an empty array throws a ValueError in PHP 8 (it raised a warning and returned false in earlier versions). Always guard against empty input:

<?php
$values = [];

$lowest = empty($values) ? null : min($values);

var_dump($lowest); // NULL
?>

When to use min()

  • Finding the cheapest price, earliest date (as a timestamp), or smallest measurement in a dataset.
  • Clamping a value to an upper bound: min($value, $maxAllowed) never lets $value exceed $maxAllowed.
  • Any time you would otherwise write a loop just to track the smallest item.

For the opposite operation, see max(). To learn more about working with the data structures min() operates on, see PHP Arrays and Sorting Arrays.

Conclusion

min() is a small but frequently used helper: pass it an array or a list of values and it returns the lowest one, keeping the original type. Remember the two call styles, watch out for cross-type comparisons, and guard against empty arrays, and it will save you from writing manual comparison loops.

Practice

Practice
What does the min() function in PHP do?
What does the min() function in PHP do?
Was this page helpful?