In PHP 8 and later, passing an empty array to min() or max() throws a ValueError. Check for an empty data set first and return a result that matches the application contract instead of relying on an exception as ordinary control flow.
Last updated: October 25, 2026.
<?php
/**
* @param list<int|float> $values
* @return array{min: int|float, max: int|float}|null
*/
function numericRange(array $values): ?array
{
if ($values === []) {
return null;
}
foreach ($values as $value) {
if (!is_int($value) && !is_float($value)) {
throw new InvalidArgumentException('Every value must be numeric.');
}
}
return [
'min' => min($values),
'max' => max($values),
];
}Here, null means that no range exists. A reporting function might return a structured “no data” response instead, while a required-data workflow could throw a domain-specific exception.
Define the empty-set result explicitly
The PHP manual states that min() and max() throw ValueError for an empty array. Before PHP 8.0, these calls returned false and emitted a warning. Code that migrated from an older runtime should not test the result against false; the call does not return normally.
A ValueError means that the argument type is acceptable but its value is not. Catch it at an integration boundary only when recovery is meaningful. Within application code, an explicit empty check documents the expected behavior and avoids hiding unrelated mistakes.
Keep values comparable
min() and max() use PHP comparison rules and return one of the original values. Mixed strings, numbers, booleans, and arrays can therefore produce results that do not represent a numeric range. Validate or normalize values before aggregation, preferably when data enters the application.
Do not replace an empty array with a magic number such as zero unless zero is a valid identity for the business calculation. For prices, timestamps, or measurements, that can silently create a false minimum or maximum. Continue with PHP array fundamentals, safe traversal of readonly arrays, and handling null array offsets.