max()
Today, we will discuss the max() function in PHP. This function is used to find the highest value in an array or a set of values.
The PHP max() function returns the highest value from a list of arguments or from a single array. It is one of the most common ways to find a maximum without writing a loop yourself. This chapter covers both call styles, how values of different types are compared, the edge cases that trip people up, and when to reach for max() versus an alternative.
Syntax
max() accepts two distinct call forms:
// Form 1: two or more values
max(mixed $value1, mixed $value2, mixed ...$values): mixed
// Form 2: a single array
max(array $value_array): mixedIn both forms the return type matches the type of the value that "wins" the comparison — pass integers and you get an integer back, pass strings and you get a string.
Finding the Highest Value in an Array
When you pass a single array, max() returns its largest element:
This works for associative arrays too. The keys are ignored; only the values are compared, and the value (not the key) is returned:
<?php
$prices = ["apple" => 1.20, "pear" => 0.95, "mango" => 2.40];
echo max($prices); // 2.4
?>Comparing a Set of Values
If you already have separate variables, pass them directly instead of building an array. This form takes any number of arguments:
<?php
echo max(10, 42, 7); // 42
echo "\n";
echo max(3, 9, 9, 1); // 9 — ties simply return the value
?>This is handy for clamping a number to a floor, a frequent pattern in pagination and form handling:
<?php
$requestedPage = -3;
// Never let the page number drop below 1
$page = max(1, $requestedPage);
echo $page; // 1
?>How max() Compares Values
max() uses PHP's standard comparison rules, so the result depends on the types involved.
- Numbers compare numerically:
max(2, 10)is10. - Strings compare alphabetically (and case-sensitively — uppercase letters sort before lowercase):
max("apple", "banana", "cherry")is"cherry". - Mixed string and number: a non-numeric string is treated as greater than
0in PHP 8, somax(0, "hello")returns"hello". Avoid mixing types when you can — the rules are easy to misread. - Arrays are compared by length first, then element by element, and an array always counts as greater than a scalar.
<?php
// Arrays of equal length: compared element by element
var_dump(max([1, 5], [2, 1]));
// Returns [2, 1] because the first element 2 > 1
?>Edge Cases and Gotchas
- Empty array:
max([])throws aValueErrorin PHP 8 (it emitted a warning and returnedfalsein PHP 7). Guard against empty input before calling it. - A single non-array argument is invalid —
max(5)raises an error. Usemax(5, $other)ormax([5]). NANvalues make the result unreliable; filter them out first.- Ties return the first-encountered winning value, so the result is stable but the original key is lost.
Related Functions
min()— the mirror image, returns the lowest value.sort()— sort an entire array when you need every element ordered, not just the extreme.array_sum()— add all elements together.count()— count elements, useful for guardingmax()against empty arrays.
Conclusion
max() is the concise, readable way to pull the highest value out of a set of arguments or an array, and it doubles as a clean clamp (max(1, $n)). Keep the values the same type, guard against empty arrays in PHP 8, and reach for min() when you need the other end of the range.