Find the largest value in an array with PHP

Josh Sherman
1 min read
Software Development PHP

Last week we discussed how to find the smallest value in an array so it seemed fitting to discuss how to find the largest value in an array. As we previously discussed back in the day you would have to loop through an array and compare values until you found the smallest (or in this case largest) value. As a complement to the min() function, we also have the max() function available:

$array   = array(99, 32, 6, 108, 144);
$largest = max($array);

Keep in mind that both min() and max() can work with arrays that contain values of mixed types. YMMV as this could yield some unexpected results if you’re not familiar with how PHP handles the comparisons of different types.

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

Related Articles