Find Highest Value Using Max Function

Find highest value using max function

When working with different types of data in PHP, it's likely that you'll need to compare two or more variables together to find either the max value or the min value. Within PHP we can use the max function to find the maximum value in a list of provided values. It's a great way (and most efficient compared to writing your own function for it) to determine the largest value from provided values. We'll explore how it works, and what types of data you can compare, including finding the largest number between two arrays, and finding the index of the highest value in an array.

How does the max function work?

The max function in PHP accepts any number of arguments and returns the highest value of the subset provided, in the same type. Because of this, the return type of max is mixed. Below are the standard uses of the max function. It works by returning what it considers the highest value. This is according to PHP's built-in standard comparison rules. It's important to note, that if comparing different input values, the first provided value will be returned, therefore giving unpredictable results.

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

In this simple below, max will return 500, because when comparing the two numbers, 500 is bigger than 100.

# Outputs: 500
max(100, 500);

Here, using floating point numbers, the max function will return 99.99, because when comparing the two floats, 99.99 is bigger than 99.95.

# Outputs: 99.99
max(99.99, 99.95);

It also works with negative numbers, where here -1 is bigger than -10.

# Outputs: -1
echo max(-1, -10);

In this simple below, max will return 500, because when comparing the two numbers, 500 is bigger than 100.

# Outputs: [0] => 8
max([2], [1], [8], [-1]);

Avoid comparing different input types because PHP will compare non-numeric strings to an int as if they are a 0, whereas multiple values of this type are compared alphanumerically. However, since PHP 8, this has somewhat been improved. Now, if strings are being compared to a number, the string is no longer converted to a number before the comparison occurs. Therefore PHP 8+ will behave differently from PHP 7 and below, as shown in this example below.

/* In PHP 8+ */
# Outputs: test
max("test", 0);

# Outputs: test
max(0, "test");

/* In PHP 7 and below */
# Outputs: "test"
max("test", 0);

# Outputs: 0
max(0, "test");

If passing an empty array to max(), this will throw a ValueError and an E_WARNING in PHP 7 and below.

How to find the biggest item in an array with PHP

The max function has a special case where it's possible to find the highest value in a single array. By just passing a single array into this function, PHP will return the highest value in that particular array, instead of if you were to pass two or more values, it would do a comparison on the multi values returning the biggest.

# Outputs: 9
max([1, 5, 9, 2, 6, 7]);

Using max with associative arrays

It is possible to use the PHP max function on assoicative arrays (an array which contain string keys instead of numeric), and it works the same as all the code examples above. The following code will output 5 as that is the larger number in the associative array provided.

# Outputs: 5
max(['Monday' => 1, 'Friday' => 5]);

How to get the array index of max value

Sometimes getting the array index instead of the array value is more useful when using the max() function. Out of the box, max doesn't provide feature but it is possible to use the max function with array_search. Coupling max with array_search will allow you to get the array index of the highest value in an array, let's view the PHP code that makes that happen. Passing a random array of numbers to the max() will output the highest value as 155. Saving this output (value) to a variable, we can use this variable to find the array index that belongs to, using array_search. By passing the orginal array and the value of max's function output, PHP will search the array for the value 155, and return the assoicated index, in this case that's 7.

$numbers = array(4 => 10, 2 => 12, 7 => 155, 1 => 21);

$maxValue = max($numbers);
# Outputs 155

$index = array_search($maxValue, $numbers);
# Outputs 7

Conclusion

Using the max function in PHP is a great way to compare two or more input values to find the biggest one. It can also be used to find the largest value in a single array, which avoids the need to sort before use.

  • Pass one array to compare itself and return the highest value
  • Avoid mismatching input values to avoid unpredictable results, however in PHP 8 and above strings are no longer converted to a number
  • The max() function works with associative arrays too
  • Use the PHP min function to find the smallest value

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all