PHP max() Function
Definition and Usage
max() returns the maximum value.
Syntax
max(x,y)
Parameter | Description |
---|---|
x | Required. A number. |
y | Required. A number. |
Description
max() returns the largest numeric value in the parameters.
If there is only one parameter and it is an array, max() returns the largest value in the array. If the first parameter is an integer, string, or floating-point number, at least two parameters are required and max() will return the largest of these values. You can compare an infinite number of values.
Tips and Comments
Note:PHP will treat non-numeric strings as 0, but if this is the largest number, it will still return a string. If multiple parameters are evaluated to 0 and are the largest, max() will return the 0 of the number, and if there is no numeric 0 in the parameters, it will return the largest string in alphabetical order.
Example
In this example, we will use max() to return the maximum of two specified numbers:
<?php echo(max(5,7)); echo(max(-3,5)); echo(max(-3,-5)); echo(max(7.25,7.30)); ?>
Output similar to:
7 5 -3 7.3