Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

abs

Returns the absolute value of a number.

Available since: PHP 4

Syntax

abs(int|float $num): int|float

Parameters

ParameterTypeDescription
$numint|floatThe 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.2

The distance between two numbers:

<?php
 
echo abs(3 - 10);

Output:

7

Notes

The function is handy when the size of a difference matters, not its sign.

See also

  • round — Rounds a number to a given number of digits.
  • max — Returns the largest of the given values.

In the tutorial

  • Operators — Arithmetic, comparison and logical operators in PHP, and operator precedence.

Thanks for reading!

Donate to the site — we work hard for you.