abs
Returns the absolute value of a number.
Available since: PHP 4
Syntax
abs(int|float $num): int|floatParameters
| Parameter | Type | Description |
|---|---|---|
| $num | int|float | The source number. |
Return value
int|floatThe number without its sign; the result has the same type as the argument.
Examples
Absolute value of an integer and of a float:
<?php
echo abs(-5), "\n";
echo abs(3.2);Output:
5
3.2The distance between two numbers:
<?php
echo abs(3 - 10);Output:
7Notes
The function is handy when the size of a difference matters, not its sign.
See also
In the tutorial
- Operators — Arithmetic, comparison and logical operators in PHP, and operator precedence.