Current Location: Home> Latest Articles> Common combination of PHP abs() and min() functions

Common combination of PHP abs() and min() functions

gitbox 2025-05-28

Introduction to abs() function

The abs() function returns the absolute value of a number. Regardless of whether the number passed in is a positive, negative or floating point number, the result returned is a non-negative number.

 <?php
echo abs(-5);    // Output 5
echo abs(3.14);  // Output 3.14
?>

This function is very useful when it is necessary to deal with distance, difference or error, because the absolute value ignores positive and negative directions and only cares about the numerical size.

Introduction to min() function

The min() function is used to find the smallest value from a set of numbers. It can receive any number of parameters, or an array as input.

 <?php
echo min(4, 7, 1, 9);          // Output 1
echo min([10, 2, 8, 6]);       // Output 2
?>

This function simplifies code logic when it is necessary to compare multiple values ​​to get the minimum value.

Use abs() and min() in conjunction

Using abs() and min() in combination is usually used to deal with the comparison of distance or errors. For example, if you want to find the number closest to a reference value among several numbers, we can first calculate the absolute distance between each number and the reference value, and then use the min() function to determine the minimum distance.

Example: Find the number closest to the reference value in the array

 <?php
$base = 10;
$numbers = [7, 14, 20, 9, 11];

// Calculate the absolute distance between each number and the reference value
$distances = array_map(function($num) use ($base) {
    return abs($num - $base);
}, $numbers);

// Find the minimum distance
$minDistance = min($distances);

// Find the closest number to the reference value
foreach ($numbers as $num) {
    if (abs($num - $base) == $minDistance) {
        echo "Closest to the baseline {$base} The number is:{$num}";
        break;
    }
}
?>

In the above code, we use abs() to calculate the distance from the reference value, then use min() to find the minimum distance, and finally determine the number closest to the reference value.

Common application scenarios

1. Distance comparison

When you need to compare the distance between multiple coordinate points and target points, you usually use abs() to calculate the absolute value of the difference, and then use min() to find the shortest distance, which is suitable for one-dimensional scenes.

2. Limited scope

During data processing, if you want to limit a certain value not to exceed the specified threshold, you can use abs() to calculate the deviation, and then select the smallest error value through min() to achieve range limitation.

3. Error comparison

In numerical calculations and experimental data analysis, it is often necessary to find the measured value with the smallest error. First use abs() to find the absolute value of the error, and then use min() to determine the minimum error.

4. Sort or filter

By calculating the absolute gap of each element, then using min() to quickly find the optimal element, and implement complex filtering logic with other functions.


Based on the above introduction, you can find that the abs() and min() functions are very flexible, which can simplify the code to implement various common comparison and restriction operations. In daily PHP development, practicing the coordinated usage of these two functions can effectively improve code quality and execution efficiency.

 <?php
// Comprehensive examples:Find all elements in the array that are closest to the target value
$target = 50;
$arr = [48, 52, 47, 51, 49, 53];

// Calculate distance array
$distances = array_map(fn($v) => abs($v - $target), $arr);

// Find the minimum distance
$minDist = min($distances);

// Filter out all elements whose distance is equal to the minimum distance
$closest = array_filter($arr, fn($v) => abs($v - $target) === $minDist);

print_r($closest);
?>

In this way, you can not only find the element closest to the target value, but also deal with multiple situations of the same distance.