php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37949 max() fails on empty arrays
Submitted: 2006-06-28 15:39 UTC Modified: 2006-06-28 17:37 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: delta407 at lerfjhax dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.1.4 OS: Linux
Private report: No CVE-ID: None
 [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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-28 17:29 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected.
 [2006-06-28 17:37 UTC] delta407 at lerfjhax dot com
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 19:01:32 2025 UTC