min() Doesn’t Accept Empty Arrays
min() doesn’t accept empty arrays anymore. It used to, and returned false, which is a type away from 0.
Nowadays, to distinguish between returned false or null and an empty array, an exception is raised. It is recommended to check the array for content before using min() or to catch the Error with a try catch.
Note that max() behave the same.
PHP code
<?php
try {
$a = min([]);
} catch (\Error $e) {
print $e->getMessage();
}
var_dump($a);
?>
Before
PHP Warning: min(): Array must contain at least one element
Warning: min(): Array must contain at least one element
bool(false)
After
min(): 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.