|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-06-28 15:39 UTC] delta407 at lerfjhax dot com
Description:
------------
max() throws a warning when given an empty array. The
documentation reads:
"If the first and only parameter is an array, max() returns
the highest value in that array. ... You can compare an
unlimited number of values."
"Unlimited number of values", by definition of unlimited,
includes zero values. (If there aren't any values in the
array, the highest value is the value null.) This is
consistent with other max() implementations -- MySQL, Oracle,
Postgres, Ruby...
Please either remove the warning and return null, or update
the documentation to reflect the existence of the warning.
Reproduce code:
---------------
<?
$a = array(1,3,2);
function foo() {
global $a;
$v = max($a);
echo is_null($v) ? "(null)\n" : "$v\n";
array_pop($a);
}
foo($a);
foo($a);
foo($a);
foo($a);
?>
Expected result:
----------------
3
3
1
(null)
Actual result:
--------------
3
3
1
PHP Warning: max(): Array must contain atleast one element in
foo.php on line 6
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Feb 10 06:00:01 2026 UTC |
This may be expected by you and other PHP developers, but again, is not present in the documentation. I double-checked the documentation, and it says nothing about raising a warning on an empty input array. In fact, it says the opposite ("can compare an unlimited number of values") -- unlimited implies that there is no artificial bound at zero values. Again, please update the documentation to reflect this.