Example 1 — Basic abs() usage
This example shows abs() with a negative integer, a positive float, a negative float and a numeric string.
<?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
- abs(-5) removes the minus sign and returns 5.
- abs(4.2) is already positive, so it stays 4.2.
- abs(-4.2) becomes 4.2 and stays a float.
- The string '-3' is converted to the number -3 first, so abs() returns 3.

