max
Returns the highest value from a list of arguments or from a single array.
max(mixed $value, mixed ...$values): mixed- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | Any comparable value. If it is the only argument and is an array, the highest element of that array is returned. |
$valuesoptional | mixed | Any number of further comparable values (variadic). |
Return value
mixedThe value considered highest 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.0max() throws a ValueError on failure; previously it returned false and emitted an E_WARNING.
- PHP 8.0.0String-to-number comparison rules changed, so max() no longer returns a different result depending on argument order in those cases.
max() has two call forms. Give it two or more arguments and it returns the largest of them; give it a single array and it returns the largest element inside that array. Mixing the two — max($array, $extra) — compares the array itself against the other value instead of looking inside it, which is almost never what you meant.
Comparison rules
Values are compared with PHP's standard comparison operator, not a numeric one. Two arrays compare first on length and then element by element, and an array is always "greater" than a non-array. A boolean drags the whole comparison into boolean space, so max(0, true) is true. Strings compare alphanumerically unless both look numeric.
The winner is returned with its original type untouched, so max('10', 9) gives back the string '10'. When several values compare equal, the first one passed wins.
Beyond finding a maximum
max($n, $floor) is the idiomatic way to clamp a value from below without an if, and min(max($n, $lo), $hi) clamps to a range. Note that max() returns the value and not its key — combine it with array_search() when you need to know which entry won.
Example
When to use it
- Finding the highest score, price or timestamp in a result set.
- Clamping a value to a minimum floor, such as max($discountedPrice, 0).
- Picking the most recent of several dates without sorting the whole list.
- Sizing a chart axis to the largest data point via max(array_column($rows, 'value')).
Frequently asked questions
How do I find the highest value in a PHP array?
Pass the array as the single argument: max($arr). Passing the array alongside other values compares the array itself rather than its elements.
How do I get the key of the maximum value?
Use array_search(max($arr), $arr), which returns the first key holding the maximum. If several keys can tie, use array_keys($arr, max($arr)) to get all of them.
What does max return for an empty array?
It throws a ValueError. PHP 7 returned false with a warning instead, so code written against the old behaviour needs an empty() or count() guard before the call.
More examples
Clamping without an if
max($v, $lo) raises anything below the floor up to it, and wrapping that in min() caps the top, so the three inputs land on 0.0, 0.5 and 10.0. The last line is the one-argument idiom for "never let this go below zero", which reads better than an if statement in an expression.
<?php
function clamp(float $v, float $lo, float $hi): float {
return min(max($v, $lo), $hi);
}
foreach ([-20.0, 0.5, 42.0] as $v) {
printf("%7.1f -> %5.1f\n", $v, clamp($v, 0.0, 10.0));
}
echo "never negative: ", max(0, -5), "\n";Finding the key, not just the value
max() gives 480 but says nothing about where it came from. array_search() finds the first key holding that value — 'south' — and silently ignores the tie. array_keys() with a search value returns every matching key, which is what you want whenever a tie is possible.
<?php
$sales = ['north' => 120, 'south' => 480, 'east' => 480, 'west' => 95];
echo "best figure: ", max($sales), "\n";
echo "first best region: ", array_search(max($sales), $sales), "\n";
print_r(array_keys($sales, max($sales)));Mixed types and the spread operator
Both spread and array_merge give 11, though merging is cheaper for large arrays. max('10', 9) compares numerically because '10' is a numeric string, but returns the original string(2) "10" rather than an int. max('apple', 'Banana') compares as strings, and lowercase 'a' outranks uppercase 'B' in byte order.
<?php
$a = [4, 9, 2];
$b = [11, 3];
echo max(...$a, ...$b), "\n";
echo max(array_merge($a, $b)), "\n";
var_dump(max('10', 9));
var_dump(max('apple', 'Banana'));
Discussion