max() function in PHP

The max() function in PHP returns the maximum value from a set of values or an array. It accepts either multiple individual values or a single array as input.

Syntax

max(value1, value2, value3, ...)
max(array_values)

Parameters

  • value1, value2, ... − Individual values to compare (can be numbers or strings).

  • array_values − An array containing values to compare.

Return Value

Returns the maximum value found. For string comparisons, it uses lexicographical ordering.

Example 1: Using Multiple Values

Finding the maximum among individual numeric values −

<?php
    echo max(70, 89, 12, 34, 23, 66, 34);
?>
89

Example 2: Using Array

Finding the maximum value from an array −

<?php
    $numbers = array(70, 89, 12, 34, 23, 66, 34);
    echo max($numbers);
?>
89

Example 3: String Comparison

The max() function also works with strings using alphabetical order −

<?php
    echo max("apple", "zebra", "banana", "orange");
?>
zebra

Conclusion

The max() function provides a simple way to find the largest value from multiple inputs or arrays. It works with both numeric and string values, making it versatile for various comparison needs.

Updated on: 2026-03-15T07:28:58+05:30

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements