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 among 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 one among 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 value, it will still return a string. If multiple parameters are evaluated to 0 and are the largest value, max() will return the numeric 0 among them. If there is no numeric 0 among the parameters, it will return the largest string in alphabetical order.
Example
In this example, we will use max() to return the maximum value 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