max() Must Contain At Least One Element¶
max() returns the largest value in the argument. When that argument is an empty array, there is an ambiguity related to the returned value, as there is no such value. PHP would return null, thought it is possible for max() to return null.
To be consistent, PHP emits an error on an empty array : it is not possible to get the maximum value when there are none.
PHP code¶
<?php
try {
$a = max([]);
} catch (\Error $e) {
print $e->getMessage();
}
var_dump($a);
?>
Before¶
PHP Warning: max(): Array must contain at least one element
Warning: max(): Array must contain at least one element
bool(false)
After¶
max(): Argument #1 ($value) must contain at least one elementPHP Warning: Undefined variable $a
Warning: Undefined variable $a
NULL
PHP version change¶
This behavior changed in 8.0