Last Updated on April 14, 2024 by Roshan Parihar
The absolute value or abs()
returns a float for a float value and an integer for an int value. It is useful to find the absolute value or positive value of a number.
What is PHP Absolute Value?
An absolute value in PHP is the positive value of the given number in integer or float format. Let’s take a simple example if you have a number -9 which is a negative number. Then, its absolute value is 9 which is a positive value.
Abs() Function in PHP to Find Absolute Values
PHP comes with an inbuilt function abs()
to find the absolute value of a number. The function takes a single argument which can be the number to pass. After you pass the number as an argument, you will get its value as positive as the result.
Examples to Get Absolute Values of a Number in PHP
Example 1
Suppose you have an integer number -9, you can find its absolute value below. As it is an integer, you will get an integer value.
1 2 3 |
<?php echo abs(-9); ?> |
Output
Example 2
Another example is a float number -3.6 and you can find its absolute value as given below. As it is a float number, you will get a float value.
1 2 3 |
<?php echo abs(-3.6); ?> |
Output
Frequently Asked Questions (FAQS)
Q1. Which PHP Function Gives Absolute Values?
Answer: abs()
is the built-in function in PHP to get the absolute value. Pass the number as the argument of the function to get the result.
Q2. How to Find Abs Integer Value in PHP?
Answer: To find the absolute int value in PHP, you can use the abs()
function and pass the integer as its argument. If -3 is an integer, you will 3 as a result after passing it as an argument of abs()
Q3. What is an Absolute Number?
Answer: Absolute number is a positive integer or float number in math.
You May Also Like to Read