Finding the smallest or largest value with PHP

Figured I would keep up the trend of talking about finding the smallest and
largest values in arrays by talking about finding the smallest or largest
value in a set of values. Incidentally, doing so will utilize the same min()
and max() functions we used previously. Not one of my favorite things, but
PHP has a tendency to allow mixed input on some functions. In this case,
min() and max() can take either a single array of input or a series of
values (which could be an array). If more than one argument is present, the
functions will determine the smallest or largest value from the passed
arguments.

// Finds the smallest value
$smallest = min('one', 50, false, array(1,2,3

// Finds the largest value
$largest = max('BIG', 'little', 3.14, true, $val

Also keep in mind that this technique is good for obtaining the smallest or
largest value but doesn’t do much for control flow. For that, you’d still want
to utilize if/else control structures.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.