Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

max

Returns the largest of the given values.

Available since: PHP 4

Syntax

max(mixed $value, mixed ...$values): mixed

Parameters

ParameterTypeDescription
$valuemixedThe first value, or an array of values.
...$valuesmixedThe other values to compare.

Return value

mixedThe largest value.

Examples

Several arguments, or a single array:

<?php
 
echo max(1, 5, 3), "\n";
echo max([2, 8, 4]);

Output:

5
8

The companion min() returns the smallest:

<?php
 
echo min(1, 5, 3);

Output:

1

Notes

Passing an empty array throws an error — check the array before the call.

See also

  • array_sum — Returns the sum of the values in an array.
  • abs — Returns the absolute value of a number.

Related recipes

In the tutorial

  • Operators — Arithmetic, comparison and logical operators in PHP, and operator precedence.

Thanks for reading!

Donate to the site — we work hard for you.