How to Find the Largest Number in an Array in PHP

PHP


In this article, we show how to find the largest number in an array in PHP.

To find the largest number in an array in PHP, we use the max() function.

Let's say we have the array of numbers below:

$numbers= array(103, 170, 210, 375, 315, 470, 255);

Let's say these numbers represent prices of airline tickets that a user looks up. To find the most expensive price, we need to be able to pick out the greatest number in the array.

We use the max() function to find the largest number in the array.

The general form to find the largest number in an array is:

$largest= max($array_name);

where $array_name is the name of the array and $largest is the variable which contains the largest value of the array.

For the above array we created, the code to find the largest number is:

$largest= max($numbers);

PHP Output

The largest value in the array is 470

Find the largest number in an array can be useful in a number of web applications. It can locate the most expensive prices or the greatest amount of anything from a list or a column in a table.

For a tutorial on finding the smallest number in an array, see How to Find the Smallest Number in an Array in PHP.

HTML Comment Box is loading comments...