Finding the smallest or largest value with PHP

Josh Sherman
1 min read
Software Development 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.

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

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.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles