abs
Returns the absolute value of a number, keeping int input as int and float input as float.
abs(int|float $num): int|float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | int|float | The numeric value to process. |
Return value
int|floatThe absolute value of num. If the argument is a float the return type is float, otherwise it is int.
Errors
Throws a TypeError if num is not an int or float and cannot be coerced to one.
Changelog
- PHP 8.0.0num no longer accepts internal objects that support numeric conversion.
abs() returns the magnitude of a number: the value with any minus sign removed. It preserves type in a way people forget — an int in gives an int out, a float in gives a float out, and a numeric string is coerced first.
When to reach for it
The most common real use is not "make it positive" but measuring how far apart two numbers are: abs($a - $b) is the distance between them regardless of order. That single expression is the basis of safe float comparison (abs($a - $b) < 1e-9), of sorting by closeness to a target, of tolerance checks in tests, and of drift or variance alerts.
What trips people up
There is exactly one input where the return type changes unexpectedly: PHP_INT_MIN. Its positive counterpart is one larger than PHP_INT_MAX, so PHP promotes the result to float and you quietly lose integer precision.
abs() is not a rounding function and not a sign function. If you want the sign itself, use the spaceship operator: $n <=> 0 gives -1, 0 or 1. And abs(-0.0) is 0.0, so negative zero disappears — useful when formatting output that would otherwise print -0.
Example
When to use it
- Compare two floats safely with abs($a - $b) < 1e-9 instead of ==.
- Sort search results by how close a price is to the shopper's target price.
- Report the size of a stock discrepancy without caring whether it is over or under.
Frequently asked questions
Does abs() always return a float?
No. abs() returns an int when given an int and a float when given a float. The one exception is abs(PHP_INT_MIN), which overflows the int range and comes back as a float.
How do I get the absolute value of a string number in PHP?
Pass it directly: abs("-12") returns int(12) and abs("-12.5") returns float(12.5), because numeric strings are coerced automatically. Under strict_types=1 you must cast the string to int or float yourself.
What is the difference between abs() and the sign of a number?
abs() throws the sign away and keeps the magnitude. To get the sign instead, use the spaceship operator: $n <=> 0 returns -1, 0 or 1.
More examples
Sort by distance from a target
The output is [19, 25, 12, 40]: 19 is one away from 20, 25 is five away, 12 is eight away and 40 is twenty away. abs() turns "nearest to" into a plain numeric sort key.
<?php
$target = 20;
$prices = [12, 25, 19, 40];
usort($prices, fn($a, $b) => abs($a - $target) <=> abs($b - $target));
print_r($prices);Compare floats with a tolerance
The strict comparison prints false because 0.1 + 0.2 is 0.30000000000000004 in binary floating point. The abs() tolerance test prints true, and the third line shows the actual gap is about 5.5e-17.
<?php
$sum = 0.1 + 0.2;
var_dump($sum === 0.3);
var_dump(abs($sum - 0.3) < 1e-9);
echo abs($sum - 0.3), PHP_EOL;
Discussion