PHP Function Reference

PHP abs() Function



The PHP abs() function returns the absolute value (positive value) of the specified number. If the argument is of type float, the return type is also float, else it is integer. In special cases it returns the following:

  • If the argument is infinite, the result is positive infinity.
  • If the argument is NAN, the result is NAN.

Syntax

abs(number)

Parameters

number Required. Specify a number.

Return Value

Returns the absolute value (positive value) of the specified number.

Example:

In the example below, abs() function is used to return the absolute value (positive value) of the specified number.

<?php
echo "abs(-10) = ".abs(-10)."\n";
echo "abs(-5.5) = ".abs(-5.5)."\n";
echo "abs(10) = ".abs(10)."\n";
echo "abs(5.5) = ".abs(5.5)."\n";
echo "abs(-INF) = ".abs(-INF)."\n";
echo "abs(NAN) = ".abs(NAN)."\n";
?>

The output of the above code will be:

abs(-10) = 10
abs(-5.5) = 5.5
abs(10) = 10
abs(5.5) = 5.5
abs(-INF) = INF
abs(NAN) = NAN

❮ PHP Math Reference