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);
Call
What 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
Parameter
Description
value
Required. Either an array of values to compare, or the first value to compare.
values
Optional. Any number of extra values to compare when the first argument is not an array.
Return Value
Return Value
Description
mixed
The 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.
Strings are compared alphabetically, so "cherry" is the highest.
max($scores) returns the highest value, 95, and ignores the keys.
array_search() finds which key holds that value.
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.