Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
PHP abs() Function
The abs() function is a built-in PHP function that returns the absolute value of a number. The absolute value is always positive, regardless of whether the input is positive or negative.
Syntax
abs(mixed $number)
Parameters
| Parameter | Description |
|---|---|
number |
The numeric value to process (int, float, or numeric string) |
Return Value
Returns the absolute value of the number. If the parameter is a float, the return type is float. For integer parameters, the return type is integer.
Examples
Basic Usage with Numbers
<?php
echo abs(-15) . "<br>"; // Integer
echo abs(15) . "<br>"; // Positive integer
echo abs(-3.14) . "<br>"; // Float
echo abs(0) . "<br>"; // Zero
?>
15 15 3.14 0
With Numeric Strings
<?php
echo abs("-25") . "<br>";
echo abs("42.5") . "<br>";
?>
25 42.5
With Non-Numeric Strings
<?php
echo abs("Hello") . "<br>";
echo abs("") . "<br>";
?>
0 0
Conclusion
The abs() function is useful for mathematical calculations where you need the distance from zero. It handles integers, floats, and numeric strings, returning 0 for non-numeric values.
Advertisements
