PHP max() Function

In this chapter you will learn:

  1. Definition for PHP max() Function
  2. Syntax for PHP max() Function
  3. Parameter for PHP max() Function
  4. Return for PHP max() Function
  5. Example - Find highest value with the max() function
  6. Example - Finding the Greatest and Least Values in a key-value pair

Definition

The max() function returns the highest value in an array, or the highest value of several values.

Syntax

PHP max() Function has the following syntax.

max(value1,value2,...);

or

max(array_values);

Parameter

ParameterIs Required Description
array_valuesRequired. An array containing the values
value1,value2,...Required. Values to compare (must be at least two values)

Return

Item Description
Return ValueThe numerically highest value
Return TypeMixed

Example 1

Find highest value with the max() function:


<?php
echo(max(2,4,6,8,10) . "\n");
echo(max(array(4,6,8,10)) . "\n");
?>

The code above generates the following result.

Example 2

Finding the Greatest and Least Values in a key-value pair


<?php//from j  a  va 2 s .c o  m
  
  $langs = array( 'PHP' => 1, 'Java' => 2, 'Java2s.com' => 3,);

  printf("<p>Most items: %d; least items: %d.</p>\n",
          max($langs), min($langs));
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP min() Function
  2. Syntax for PHP min() Function
  3. Parameter for PHP min() Function
  4. Return for PHP min() Function
  5. Example - Find lowest value with the min() function
  6. Example - Finding the Greatest and Least Values in a key-value pair