PHP max() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

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

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

Syntax

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

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

max() returns the original value, so the type stays the same (int stays int, float stays float). With an array, it needs at least one element, otherwise PHP 8 throws a ValueError.

Library declarations
PHP
max(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 highest value found, returned exactly as it was passed in.

PHP Examples

Example 1 — Find the Largest Number

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

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

How It Works

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

Example 2 — Empty Array Throws an Error

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

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

How It Works

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

Example 3 — Strings and Associative Arrays

max() also compares strings alphabetically and can find the top score in an array.

PHP
<?php
echo max("apple", "banana", "cherry"), PHP_EOL;

$scores = ["Ana" => 82, "Ben" => 95, "Cara" => 78];
echo max($scores), PHP_EOL;
echo array_search(max($scores), $scores), PHP_EOL;

How It Works

  1. Strings are compared alphabetically, so "cherry" is the highest.
  2. max($scores) returns the highest value, 95, and ignores the keys.
  3. array_search() finds which key holds that value.
  4. The output shows the top score and the name of the student.

Frequently Asked Questions

max() returns the highest value from the values or array you give it. It works with numbers and strings.
Pass the array directly, for example max($numbers). PHP checks every element and returns the biggest one.
PHP 8 throws a ValueError. Check that the array has items first, for example with if (!empty($list)).
max() returns only the value. Use array_search(max($array), $array) to get the matching key.
Yes. Strings are compared using PHP comparison rules, so "cherry" is higher than "banana". Mixing numbers and strings can give surprising results, so keep types consistent.

Next: min()

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

min() 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