Definition and Usage
The max () function returns highest element in array, or highest amongst two or more comma separated parameters.
Syntax
max ( array $values ) : mixed
Or
max ( mixed $value1 [, mixed $... ] ) : mixed
Parameters
Sr.No | Parameter & Description |
---|---|
1 | values If only one parameter is given, it should be an array of valaues which may be of same or different types |
2 | value1, value2, .. If two or more parameters aregiven, they should be any comparable values of same or different types |
Return Values
PHP max() function returns highest value from the array parameter or sequence of values. Standard comparison operators are applicable. If multiple values of different types evaluate as equal (e.g. 0 and 'PHP') the first parameter to the function will be returned.
PHP Version
This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.
Example
Following example returns highest value from numeric array.
<?php $arg=array(23, 5.55, 142, 56, 99); echo "array="; foreach ($arg as $i) echo $i . ","; echo "\n"; echo "max = " . max($arg); ?>
Output
This will produce following result −
array=23,5.55,142,56,99, max = 142
Example
Following example returns max() from array of strings.−
<?php $arg=array("Java", "Angular", "PHP", "C", "Kotlin"); echo "array="; foreach ($arg as $i) echo $i . ","; echo "\n"; echo "max = " . max($arg); ?>
Output
This will produce following result −
array=Java,Angular,PHP,C,Kotlin, max = PHP
Example
Series of string values are provided to max() function in following example;
<?php $val1="Java"; $val2="Angular"; $val3="PHP"; $val4="C"; $val5="Kotlin"; echo "values=" . $val1 . "," . $val2 . "," . $val3 . "," . $val4 . "," . $val5 . "\n"; echo "max = " . max($val1, $val2, $val3,$val4,$val5); ?>
Output
This will produce following result −
values=Java,Angular,PHP,C,Kotlin max = PHP
Example
In following example array is a collection of mixed data types −
<?php $arg=array(23, "Java", 142, 1e2, 99); echo "array="; foreach ($arg as $i) echo $i . ","; echo "\n"; echo "max = " . max($arg); ?>
Output
This will produce following result −
array=23,Java,142,100,99, max = 142