joshtronic

in Software Development #PHP

Find the largest value in an array with 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.