Using abs In PHP Programming

When the result of your mathematical PHP calculations results in a value that must reflect a net change as an absolute value, the abs command is used. PHP interprets the result of the value inside the () after the abs as a numeric result and simply converts negative values to positive, and ignores positive values. abs(-5) results in 5. abs(5+1-7) results in 7, not -7. If the calculation returns a floating value, the result will also be a floating value.

The PHP commands gmp_abs() and variant_abs() operate the same way as abs() by returning the absolute value. There are other ways to use PHP to calculate the absolute value of the result of a mathematical calculation. There are situations where you should test multiple values for their type and whether they are negative or positive, before starting your calculations. Other situations may require testing the negative or positive status of the value for a subsequent command set, then convert to the absolute value for storage or another subsequent command set.

Leave a Reply