What is abs() in PHP?

The abs() function returns the absolute value of a number.

Figure 1 below shows the mathematical representation of the abs() function.

Figure 1: Mathematical representation of abs() function

Syntax


number abs(num)
#  num is the number whose absolute value is required

Parameter

The abs() function takes a number as a parameter.

Return value

The abs() function returns the absolute value of a number passed as a parameter.


If a string is passed to this function, it returns 0.

Code

<?php
#Positive number
echo("abs(10): ");
echo (abs(10));
?>
<?php
#Negative number
echo("abs(-10): ");
echo (abs(-10));
?>
<?php
#zero
echo("abs(0): ");
echo (abs(0));
?>
<?php
#string
echo("abs('hello): ");
echo (abs('hello'));
?>

Free Resources