W3docs

abs()

Today, we will discuss the abs() function in PHP. This function is used to get the absolute value of a number.

The abs() function returns the absolute value of a number — its magnitude without a sign. In other words, it tells you how far the number is from zero, so abs(-5) and abs(5) both give 5. This page covers the syntax, return types, edge cases worth knowing, and the most common real-world uses.

Syntax

abs(int|float $num): int|float
  • $num — the number you want the absolute value of (integer or float).
  • Return value — the absolute value of $num. The return type matches the input type: pass an integer, get an integer; pass a float, get a float.

Basic example

php— editable, runs on the server

This prints 5. We start with a negative value, call abs() to strip the sign, and echo the result. A positive input would be returned unchanged.

Integers vs. floats

abs() preserves the numeric type, which matters when you compare results or format output:

<?php
echo abs(-7);      // 7   (int)
echo "\n";
echo abs(-7.5);    // 7.5 (float)
echo "\n";
var_dump(abs(-7));   // int(7)
var_dump(abs(-7.5)); // float(7.5)
?>

Numeric strings are accepted too — abs("-3.2") returns the float 3.2 — but passing a non-numeric string triggers a TypeError in PHP 8+, so validate input with is_numeric() when it comes from outside your code.

A practical use: distance between two values

The most common reason to reach for abs() is to measure the difference between two numbers without caring which one is larger:

<?php
$expected = 100;
$actual   = 87;

$difference = abs($expected - $actual);

echo "You are off by {$difference}.";
?>

This prints You are off by 13. whether $actual is 87 or 113 — the order of the subtraction no longer matters. The same pattern is handy for tolerances (if (abs($a - $b) < 0.001)), price deltas, and coordinate distances.

Edge cases and gotchas

  • Zero stays zero. abs(0) is 0 and abs(-0.0) is 0.0.
  • Already positive? abs() returns it untouched, so it is safe to call defensively without checking the sign first.
  • PHP_INT_MIN overflow. abs(PHP_INT_MIN) cannot fit in an integer (its positive counterpart is one larger than PHP_INT_MAX), so PHP returns it as a float to avoid overflow.
  • It does not round. abs() only removes the sign. To drop the decimal part use floor(), ceil(), or round().
  • round() — round a float to a given precision.
  • floor() / ceil() — round down / up to the nearest integer.
  • pow() and sqrt() — exponentiation and square roots.
  • max() / min() — pick the largest or smallest of several values.
  • is_numeric() — check a value is a number before doing math on it.

For a broader tour of PHP's numeric helpers, see the PHP Math reference.

Conclusion

abs() is a small but frequently used tool: it returns a number's magnitude regardless of sign, keeps the input's type, and shines when you need the distance between two values. Remember it does not round, and watch the PHP_INT_MIN edge case where the result comes back as a float.

Practice

Practice
What is the role of the abs() function in PHP?
What is the role of the abs() function in PHP?
Was this page helpful?