max

(PHP 3, PHP 4, PHP 5)

max -- 找出最大值

说明

mixed max ( number arg1, number arg2 )

mixed max ( array numbers [, array ...] )

max() 返回参数中数值最大的值。

如果仅有一个参数且为数组,max() 返回该数组中最大的值。如果第一个参数是整数、字符串或浮点数,则至少需要两个参数而 max() 会返回这些值中最大的一个。可以比较无限多个值。

注: PHP 会将非数值的 string 当成 0,但如果这个正是最大的数值则仍然会返回一个字符串。如果多个参数的值都是 0max() 将返回第一个(最左边的值)。

例子 1. 使用 max() 的例子

<?php
echo max(1, 3, 5, 6, 7);  // 7
echo max(array(2, 4, 5)); // 5
echo max(0, 'hello');     // 0
echo max('hello', 0);     // hello
echo max(-1, 'hello');    // hello

// 对多个数组,max 从左向右比较。
// 因此在本例中:2 == 2,但 4 < 5
$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)

// 如果同时给出数组和非数组作为参数,则总是将数组视为
// 最大值返回
$val = max('string', array(2, 5, 7), 42);   // array(2, 5, 7)
?>

参见 min()count()


add a note add a note User Contributed Notes
johnphayes at gmail dot com
03-May-2006 12:27
Regarding boolean parameters in min() and max():

(a) If any of your parameters is boolean, max and min will cast the rest of them to boolean to do the comparison.
(b) true > false
(c) However, max and min will return the actual parameter value that wins the comparison (not the cast).

Here's some test cases to illustrate:

1.  max(true,100)=true
2.  max(true,0)=true
3.  max(100,true)=100
4.  max(false,100)=100
5.  max(100,false)=100
6.  min(true,100)=true
7.  min(true,0)=0
8.  min(100,true)=100
9.  min(false,100)=false
10. min(100,false)=false
11. min(true,false)=false
12. max(true,false)=true
tim at (NOSPAM) dot crazynot2 dot com
08-Nov-2005 05:56
In response to the previous two posters (zher0 at netcarrier dot com & walkingmantis):

I was trying to do exactly what zher0 suggested; calculate the max value of a multi-dimensional array with variably sized 'sub-arrays'.  Here is a simple little function I came up with to do just that:

<?php
function multimax( $array ) {
  
// use foreach to iterate over our input array.
  
foreach( $array as $value ) {
      
      
// check if $value is an array...
      
if( is_array($value) ) {
          
          
// ... $value is an array so recursively pass it into multimax() to
           // determine it's highest value.
          
$subvalue = multimax($value);
          
          
// if the returned $subvalue is greater than our current highest value,
           // set it as our $return value.
          
if( $subvalue > $return ) {
              
$return = $subvalue;
           }
      
       } elseif(
$value > $return) {
          
// ... $value is not an array so set the return variable if it's greater
           // than our highest value so far.
          
$return = $value;
       }
   }
  
  
// return (what should be) the highest value from any dimension.
  
return $return;
}
?>

Please note that I have only performed very limited testing on this code -- be sure to check it thoroughly if you implement it somewhere!
nonick AT 8027 DOT org
17-Dec-2003 11:50
If you are working with numbers, then you can use:

   $a = ($b > $c) ? $b : $c;

which is somewhat faster (roughly 16%) than

   $a = max($b, $c);

I tested this on several loops using integers and floats, over 1 million iterations.

I'm running PHP 4.3.1 as a module for Apache 1.3.27.
mikhail_kovalev at mail dot ru
14-May-2003 07:32
Note that in version 4.0.3 (the only version I tested):

max (0, 0); // returns 0.
max (0, false); // returns 0.
max (false, 0); // returns false.
max (false, false); // returns false.

As a solution use this:

(int) max (false, 0); // returns 0.
(int) max (false, false); // returns 0.