當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP Max()用法及代碼示例


max() 函數用於查找最大值。

用法:

max(array_values);

OR

max(value1,value2,value3,value4...);
名稱 描述 必需/可選
array_values 指定包含值的數組 Required
值1、值2、值3、值4... 指定要比較的值(必須至少有兩個值 Required
PHP math Max function

示例 1

<?php
$num=max(4,14,3,5,14.2);
echo "Your number is =max(4,14,3,5,14.2)".'<br>';
echo "By using max function Your number is:".$num;
?>

輸出:

Your number is =max(4,14,3,5,14.2)
By using max function Your number is:14.2

例2

<?php
$num=max(.1, .001, .2, -.5);
echo "Your number is =max(.1, .001, .2, -.5)".'<br>';
echo "By using max function Your number is:".$num;
?>

輸出:

Your number is =max(.1, .001, .2, -.5)
By using max function Your number is:0.2

例3

<?php
$arr= array(110, 20, 52 ,105, 56, 89, 96);
echo "Your number is =array(110, 20, 52 ,105, 56, 89, 96)".'<br>';
echo "By using max() function Your number is:".max($arr);
?>

輸出:

Your number is =array(110, 20, 52 ,105, 56, 89, 96)
By using max() function Your number is:110

例4

<?php
$arr= array(200, 120, 52 ,105, 56, 89, 94);
//using forloop find the max value
$b = 0;
for($i=0;$i<count($arr);$i++)
 {
    if ($arr[$i] > $b)
	{
        $b = $arr[$i];
    }
}	
echo "By using max() function Your number is:".$b;
?>

輸出:

By using max() function Your number is:200






相關用法


注:本文由純淨天空篩選整理自 PHP Max() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。