Current Location: Home> Latest Articles> Notes on using PHP abs() function and four operations

Notes on using PHP abs() function and four operations

gitbox 2025-05-26

Introduction to abs() function

abs() is a built-in function in PHP, which is used to return the absolute value of a number, that is, no matter whether the input number is positive or negative, it will return its non-negative value.

Sample code:

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

Can abs() be used together with addition, subtraction, multiplication and division?

The answer is yes. The return value of abs() is a numerical type (integer or floating point type), which can fully participate in addition, subtraction, multiplication and division operations. You can use abs() and other arithmetic operators in the expression, and PHP will calculate according to normal operation priority.

Example:

<code> <?php $a = -15; $b = 4;

// Calculate the absolute value first, and then perform the addition operation
$result = abs($a) + $b; // 15 + 4 = 19
echo $result;

// Combined with multiplication
$result2 = abs($a) * 2; // 15 * 2 = 30
echo $result2;
?>
</code>

Points to be paid attention to when using

  1. Operation order <br> The result of the function call is calculated first and then participated in the operation. Be careful not to use abs() and operator separately to cause confusion in priority, and use brackets to clarify the expression order if necessary.

    For example:

    <code> <?php $x = -8; $y = 3; $z = abs($x + $y); // Calculate x+y = -5 first, and then abs(-5) = 5 echo $z;

    $z2 = abs($x) + $y; // abs(-8)=8 + 3 = 11
    echo $z2;
    ?>
    </code>

  2. Incoming parameter type
    The abs() function accepts integer and floating point parameters. If a non-numeric type is passed in (such as a string containing non-numeric characters), PHP will automatically try to convert, but may produce unexpected results.

  3. Negative number zero and floating point accuracy
    PHP's abs() process for negative zero and floating point numbers complies with the IEEE floating point specification, but in extreme cases, accuracy errors may occur.

  4. Avoid meaningless calls <br> When explicitly knowing that the value is positive, you don't have to call abs() every time to avoid unnecessary function overhead.

Application examples in actual development

Suppose you have a scenario where you calculate the discount price of a product, and the discount amount may be negative (representing a discount). In order to ensure that the calculation results are correct, you can write it like this:

<code> <?php $price = 100; $discount = -15; // Negative value indicates a discount of 15 yuan

$final_price = $price + abs($discount);
echo $final_price; // 100 + 15 = 115. In actual logic, you need to judge whether to use abs() based on your needs.
?>
</code>

Pay attention to whether the logic is reasonable here. If the discount is negative, it directly adds the absolute value to the price may cause the price to rise in reverse, so the business logic should clarify the meaning of the discount.