Select Page

The php abs function is a part of php math and is usually used to convert negative numbers to positive numbers. Basically it gives you the absolute value of a number.

Example

[php]
<?php
echo (abs("-5.25")."n");
echo (abs("5.25")."n");
echo (abs("-5")."n");
echo (abs("5")."n"); ?>
[/php]

Output

[php]
// Output
5.25
5.25
5
5
[/php]

Definition And Usage Of the abs function

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

Syntax

[php]
abs(number);
[/php]

[table id=16 /]

Few Technical Details

[table id=17 /]

Converting Negative Numbers To Positive Numbers

For converting a negative number to a positive number the you just need to pass a number to the abs() function. You can pass any float / int to this php function. Here is an example of how you can do it –

[php]
$num = -1.5 ;
$pos_num = abs($num);

echo $pos_num ;
[/php]

I hope you understood and implemented the function properly. In case if you didn’t understand anything or you need some help related to this function feel free to ask me in the comments section below. Few other amazing php tutorials for you –

Need Help? Chat with us
Share This