PHP abs() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP abs() function returns the absolute value of a number, which means the number without its minus sign.

Use it when you only care about how big a number is, not whether it is positive or negative, like finding the distance between two values.

Syntax

Call abs() with the values you need. Start with this line.

PHP
$result = abs($num);
CallWhat it does
abs(-7)Returns 7 because the minus sign is removed.
abs(3.5)Returns 3.5 because positive numbers stay the same.
abs(-0.25)Returns 0.25, so decimals work too.

abs() keeps the type of the number: an int stays an int and a float stays a float.

Library declarations
PHP
abs(int|float $num): int|float

Parameters

ParameterDescription
numRequired. The number you want the absolute value of. Numeric strings like '-3' are converted to a number automatically.

Return Value

Return ValueDescription
int|floatThe absolute (non-negative) value of $num. Returns an int for int input and a float for float input.

PHP Examples

Example 1 — Basic abs() usage

This example shows abs() with a negative integer, a positive float, a negative float and a numeric string.

PHP
<?php
echo abs(-5) . PHP_EOL;
echo abs(4.2) . PHP_EOL;
echo abs(-4.2) . PHP_EOL;
echo abs('-3') . PHP_EOL;

How It Works

  1. abs(-5) removes the minus sign and returns 5.
  2. abs(4.2) is already positive, so it stays 4.2.
  3. abs(-4.2) becomes 4.2 and stays a float.
  4. The string '-3' is converted to the number -3 first, so abs() returns 3.

Example 2 — Edge cases with abs()

Here we check what abs() does with the smallest possible integer, negative zero and plain zero. We use var_dump() so the type is visible.

PHP
<?php
$min = PHP_INT_MIN;

var_dump(abs($min));
var_dump(abs(-0.0));
var_dump(abs(0));

How It Works

  1. PHP_INT_MIN is -9223372036854775808, and its positive version is too big for an int.
  2. Because of that, PHP automatically returns a float for abs(PHP_INT_MIN).
  3. abs(-0.0) returns float(0), so the negative sign on zero is removed.
  4. abs(0) returns int(0) because the input was an int.

Example 3 — Distance between two numbers

A common real-world use of abs() is finding how far apart two values are, no matter which one is bigger.

PHP
<?php
$a = 3;
$b = 8;
$distance = abs($a - $b);

echo "Distance between $a and $b is $distance" . PHP_EOL;

$temps = [-3, 5, -12];
foreach ($temps as $t) {
    echo "Temperature $t is " . abs($t) . " degrees from zero" . PHP_EOL;
}

How It Works

  1. We subtract $b from $a, which gives -5.
  2. abs() turns -5 into 5, so the order of $a and $b does not matter.
  3. The foreach loop goes through each temperature in the array.
  4. abs($t) prints how far each temperature is from zero without the sign.

Frequently Asked Questions

abs() returns the absolute value of a number. That means it removes the minus sign, so -10 becomes 10 and 10 stays 10.
It returns the same type as the input. An int gives an int and a float gives a float, except for abs(PHP_INT_MIN) which returns a float.
Yes, if the string is numeric like '-3' or '4.5'. PHP converts it to a number first. A non-numeric string like 'abc' throws a TypeError.
Subtract one from the other and wrap the result in abs(), like abs($a - $b). This always gives a positive distance.
It returns float(0), which is plain positive zero. abs() removes the sign even from negative zero.

Next: acos()

acos() function returns the arc cosine of a number, which is the angle whose cosine is tha.

acos() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful