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 value among the parameters.

If only one parameter is an array, max() returns the largest value in that 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 number, it will still return a string. If multiple parameters are evaluated to 0 and are the largest, max() will return the numerical 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 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