PHP function abs()

Available in PHP 4.0, PHP 5, PHP 7

Absolute value of the number

Parameters

Parameter Data type Default value Note
$number mixed not Numeric conversion value
php
$cislo = -15.4;
$abs = abs($cislo); // 15.4

Returns the absolute value of the number.

Return values

number (int, float, ...)

  • Returns the absolute value of a number (i.e. unsigned).
  • If the input is a float, it will also return a float.
  • If the input is an integer, it returns an integer.
  • If the input is integer greater than the contents of integer, it will be mapped to float.
php
abs(5); // 5
abs(-3); // 3
abs(-3.14); // 3.14

Example

php
$abs = abs(-4.2); // $abs = 4.2; (double/float)
$abs2 = abs(5); // $abs2 = 5; (integer)
$abs3 = abs(-5); // $abs3 = 5; (integer)

The return values are listed in the comment

Other sources

[Official abs function documentation](PHP.net documentation)

Newsletter

Nejlepsi tipy a triky o PHP do Vaseho e-mailu. Clanky a novinky nejen ze sveta PHP a programovani.