PHP abs() Function

The PHP abs() function returns the absolute value of a specified number as its parameter. For example:

<?php
   $x = 10;
   echo abs($x);
   echo "<br>";

   $x = -10;
   echo abs($x);
   echo "<br>";

   $x = 0;
   echo abs($x);
   echo "<br>";
?>

The output of above PHP example, is:

php abs function

Note - Absolute value is the distance from zero along real number line. Therefore, we can say that an absolute value of a number is its non-negative form.

The same (previous) program can also be created in this way:

<?php
   echo abs(10)."<br>";
   echo abs(-10)."<br>";
   echo abs(0)."<br>";
?>

You will get exactly same output as of previous one.

PHP abs() Function Syntax

The syntax of abs() function in PHP, is:

abs(x)

The value of x may be an integer or a floating-point number.

PHP Online Test


« PHP Tutorial PHP Test »

Follow/Like Us on Facebook




Subscribe Us on YouTube