Using PHP min() Function: Basics, Arrays, Strings, Empty Values, Non-numeric and Associative Arrays
The article explains how PHP's min() function returns the smallest value from multiple arguments or an array, covering its behavior with numbers, strings, empty inputs, non‑numeric data, and associative arrays, and provides clear code examples for each case.
The PHP min() function returns the smallest value among a set of numbers, accepting any number of arguments or a single array.
When called with multiple scalar arguments, it returns the minimum of those values, as shown in the example:
$min_value = min($num1, $num2, $num3);When a single array is passed, min() evaluates the array elements and returns the smallest element:
$numbers = array(10, 5, 8, 3);
$min_value = min($numbers);String arguments are compared lexicographically, so the function returns the smallest string according to dictionary order:
$min_value = min('10', '5', '8', '3');If an empty array or empty string is provided, the function returns null or an empty string respectively:
$empty_array = array();
$empty_string = '';
$min_array = min($empty_array); // returns null
$min_string = min($empty_string); // returns empty stringWhen the argument is an array containing non‑numeric values, min() also returns null :
$non_numeric_values = array('apple', 'banana', 'orange');
$min_value = min($non_numeric_values); // returns nullFor associative arrays, min() returns the lowest value among the values, ignoring keys:
$prices = array('apple' => 0.5, 'banana' => 0.3, 'orange' => 0.4);
$min_price = min($prices); // returns 0.3In summary, min() can handle multiple arguments, arrays, strings, empty inputs, non‑numeric data, and associative arrays, returning the appropriate minimum or null when applicable.
Recommended PHP learning resources include tutorials on Vue3+Laravel8+Uniapp, Vue3+TP6 API social e‑commerce development, Swoole, and Workerman+TP6 instant messaging systems (links provided).
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.