Get the maximum value from an array in PHP

Suppose we have an array that contains some numbers. Now we need to find the maximum value or number from that array in PHP.

In this article, we are going to see the easiest way to find the largest number from a PHP array. To do this we are going to use the in-built PHP max() function.

Using the max() function, we can do our task easier. All we need to do is just to pass the array that contains some numbers.

Below is the syntax of the PHP max() function:

max(array_values);

The function used to return the maximum value exists in an array. The function has been introduced from the PHP version 4 and available in PHP 4 or higher version.

Now see a real example below:

<?php
  $array_values = array(158,895,362,425,898,595,600);
  echo(max($array_values)); // Output: 898
?>

The example we can see above will show the highest number from the array. In our array, we have taken some numbers. When we pass our array in the max() function, it returns the largest number.

Our task is done just within two lines of code. Well, we can also do it just within in one line of code:

echo(max( array(158,895,362,425,898,595,600) ));

The only difference In the above code is that we have passed the array directly in the max() function instead of taking it in a variable.

Also, read:

So I hope, you have how the PHP max() function works and how we can get the maximum number value from an array in PHP using this amazing function.

Leave a Reply

Your email address will not be published. Required fields are marked *