PHP abs syntax
abs(int|float $num): int|float
In PHP, abs returns the magnitude of a number. For example, the absolute value of −6 is 6, and the absolute value of 0 is 0.
abs
<?php
echo (abs(-3)), PHP_EOL;
3
abs(int|float $num): int|float
| Input | An int or float. |
|---|---|
| Result | An int or float. The magnitude of PHP_INT_MIN is returned as a float. |
A shop records a surplus as a positive count and a shortage as a negative count. The reconciliation report asks how many items differ in each case, regardless of direction.
<?php
class MathExample {
public function getAbsoluteValue() {
// Positive integer value
$n = abs(9);
echo("The absolute value of 9 is " . $n . "\n");
// Negative integer value
$x = abs(-6);
echo("The absolute value of -6 is " . $x . "\n");
}
}
$obj = new MathExample();
$obj->getAbsoluteValue();
The absolute value of 9 is 9 The absolute value of -6 is 6
A scale reports deviations on either side of its reference weight. Find the size of each deviation while keeping the fractional part so small differences remain visible.
<?php
class MathExample {
public function getAbsoluteValue() {
// Positive floating point value
$n = abs(9.3);
echo("The absolute value of 9.3 is " . $n . "\n");
// Negative floating point value
$x = abs(-6.3);
echo("The absolute value of -6.3 is " . $x . "\n");
}
}
$obj = new MathExample();
$obj->getAbsoluteValue();
The absolute value of 9.3 is 9.3 The absolute value of -6.3 is 6.3
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
<?php
class MathExample {
public function getAbsoluteValue() {
// Positive ∞ infinite value
$n = abs(INF);
echo("The absolute value of INF is " . $n . "\n");
// Negative ∞ infinite value
$x = abs(-INF);
echo("The absolute value of -INF is " . $x . "\n");
}
}
$obj = new MathExample();
$obj->getAbsoluteValue();
The absolute value of INF is INF The absolute value of -INF is INF
A stocktake compares what the scanner found with what the inventory system expected. The report needs to distinguish a shortage, a surplus and an exact match before deciding whether to investigate.
<?php
$scanned = 47;
$expected = 52;
echo abs($scanned - $expected), PHP_EOL;
5
The scanner found 47 items against 52 on the manifest. The discrepancy is five items. Keep the signed difference separately to distinguish a shortage from a surplus.
A school library expects 48 loan chargers on its shelf after class. A student counts 45. The handover form has a separate field for the size of the discrepancy, whether the shelf is short or over.
<?php
$expected = 48;
$counted = 45;
echo abs($counted - $expected), " chargers";
3 chargers
The signed difference is minus three. Its magnitude gives the three chargers the next shift needs to account for.
A class logs the courtyard temperature at sunrise and again before lunch. The report asks how far apart the readings are, even when one is below freezing.
The two readings sit seven one-degree intervals apart on the same scale.
<?php
$sunrise = -3;
$midday = 4;
echo abs($midday - $sunrise), " °C", PHP_EOL;
7 °C
Reference: PHP numeric operations