PHP Tutorial - PHP abs() Function






Definition

The abs() function returns the absolute value of a number.

Syntax

PHP abs() Function has the following syntax.

abs(number);

Parameter

ParameterDescription
numberRequired. A number.

Return

PHP abs() function returns the absolute value of the number.

If the number is of type float, the return type is also float, otherwise it is integer.





Example

You can either send a floating-point number or an integer to abs(), and it will return the same type:


<?PHP
    echo abs(50); // 50 
    echo "\n";
    echo abs(-12); // 12 
    echo "\n";
    echo abs(50.1); // 50.1 
    echo "\n";
    echo abs(-12.5); // 12.5 
?>

The code above generates the following result.