1.2.816. No Max On Empty Array¶
Using max() or min() on an empty array leads to a valueError
exception.
Until PHP 8, max() and min() would return null in case of empty array. This might be confusing with actual values, as an array can contain null
. null
has a specific behavior when comparing with other values, and should be avoided with max() and sorts.
Until PHP 8.0, a call on an empty array would return null, and a warning.
<?php
// Throws a value error
$a = max([]);
$array = [];
if (empty($array)) {
$a = null;
} else {
$a = max($array);
}
var_dump(min([-1, null])); // NULL
var_dump(max([-1, null])); // -1
var_dump(max([1, null])); // 1
?>