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.

php Courses
php Courses
php Courses
Using PHP min() Function: Basics, Arrays, Strings, Empty Values, Non-numeric and Associative Arrays

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 string

When 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 null

For 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.3

In 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).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPCode ExamplesArraysmin functionstring-comparisonassociative array
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.