PHP min() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, min() returns the lowest value from a list of values or from an array.

You can call it with several separate values, like min(3, 9, 5), or pass a single array. It works with numbers and strings.

Syntax

Call min() with the values you need. Start with this line.

PHP
$lowest = min(3, 9, 5);
CallWhat it does
min(3, 9, 5)Returns 3, the smallest of the three values.
min([4, 12, 7])Returns 4, the smallest item in the array.
min(2.5, 2)Returns 2.

min() returns the original value, so the type stays the same. It is often combined with max() to keep a number inside a range. An empty array throws a ValueError in PHP 8.

Library declarations
PHP
min(mixed $value, mixed ...$values): mixed

Parameters

ParameterDescription
valueRequired. Either an array of values to compare, or the first value to compare.
valuesOptional. Any number of extra values to compare when the first argument is not an array.

Return Value

Return ValueDescription
mixedThe lowest value found, returned exactly as it was passed in.

PHP Examples

Example 1 — Find the Smallest Number

Pass separate values or a single array to find the lowest one.

PHP
<?php
echo min(3, 9, 5), PHP_EOL;
echo min([4, 12, 7]), PHP_EOL;
echo min(2.5, 2), PHP_EOL;

How It Works

  1. min(3, 9, 5) compares three separate values and returns 3.
  2. min([4, 12, 7]) searches the array and returns 4.
  3. min(2.5, 2) compares a float and an int and returns 2.
  4. Each result keeps its original type.

Example 2 — Empty Array Throws an Error

In PHP 8, min() cannot work on an empty array. Catch the ValueError to handle it.

PHP
<?php
try {
    echo min([]), PHP_EOL;
} catch (ValueError $e) {
    echo "Error: ", $e->getMessage(), PHP_EOL;
}

How It Works

  1. Start a try block and call min() with an empty array.
  2. There is nothing to compare, so PHP throws a ValueError.
  3. The catch block stores the error object in $e.
  4. getMessage() prints what went wrong.

Example 3 — Keep a Value Between 0 and 100

Combine min() and max() to clamp a number inside a range.

PHP
<?php
$values = [150, -20, 50];

foreach ($values as $v) {
    echo $v, " -> ", min(max($v, 0), 100), PHP_EOL;
}

How It Works

  1. Create an array with a value that is too high, one too low, and one fine.
  2. max($v, 0) makes sure the value is at least 0.
  3. min(..., 100) then makes sure the value is at most 100.
  4. Values inside the range stay unchanged.

Frequently Asked Questions

min() returns the lowest value from the values or array you give it. It works with numbers and strings.
Pass the array directly, for example min($prices). PHP checks every element and returns the smallest.
PHP 8 throws a ValueError. Check that the array has items first.
Use min(max($value, $low), $high). In newer PHP versions you can also use clamp() if it is available.
min() returns only the value. Use array_search(min($array), $array) to find the matching key.

Next: octdec()

octdec() converts an octal (base 8) number to a decimal (base 10) number.

octdec() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful