The max() function returns the highest value in an array, or the highest value of several values.
PHP max() Function has the following syntax.
max(value1,value2,...);
or
max(array_values);
Parameter | Is Required | Description |
---|---|---|
array_values | Required. | An array containing the values |
value1,value2,... | Required. | Values to compare (must be at least two values) |
Item | Description |
---|---|
Return Value | The numerically highest value |
Return Type | Mixed |
Find highest value with the max() function:
<?php
echo(max(2,4,6,8,10) . "\n");
echo(max(array(4,6,8,10)) . "\n");
?>
The code above generates the following result.
Finding the Greatest and Least Values in a key-value pair
<?php
$langs = array( 'PHP' => 1, 'Java' => 2, 'Java2s.com' => 3,);
printf("<p>Most items: %d; least items: %d.</p>\n",
max($langs), min($langs));
?>
The code above generates the following result.