PHP Australia Conference 2015

abs

(PHP 4, PHP 5)

absمقدار قدر مطلق

Description

number abs ( mixed $number )

بازگرداندن قدر مطلق number.

Parameters

number

مقدار عددی برای پردازش

Return Values

قدر مطلق number. اگر آرگومان number از نوع float باشد نوع بازگشتی نیز float است در غیراین صورت integer (همانند float معمولا بازه مقدار بیشتری نسبت به integer دارد).

Examples

Example #1 مثال abs()

<?php
$abs 
abs(-4.2); // $abs = 4.2; (double/float)
$abs2 abs(5);   // $abs2 = 5; (integer)
$abs3 abs(-5);  // $abs3 = 5; (integer)
?>

See Also

add a note add a note

User Contributed Notes 3 notes

up
-7
alex.khimich.org
11 months ago
Few ways to convert values to negative
<?php

// Multiplying by "-1"
$v = -1 * abs($v);

// Using ternary operator
$v = $v <= 0 ? $v : -$v;

?>
up
-7
Ister
6 years ago
[*EDIT* by danbrown AT php DOT net: Merged user's corrected code with previous post content.]


jeremys indicated one thing - there is no sgn function wich actually seems a bit strange for me. Of course it is as simple as possible, but it is usefull and it is a standard math function needed occasionally.

Well, I have solved this function in a bit different matter:

<?php

function sgn($liczba)
{
    if(
$liczba>0)
       
$liczba=1;
    else if(
$liczba<0)
       
$liczba=-1;
    else if(!
is_numeric($liczba))
       
$liczba=null;
    else
       
$liczba=0;
    return
$liczba;
}

?>

The difference is that it returns null when the argument isn't a number at all.
up
-17
svein dot tjonndal at gmail dot com
3 years ago
If you don't have/want GMP and are working with large numbers/currencies:

<?php
function mb_abs($number)
{
  return
str_replace('-','',$number);
}
?>

No need to worry about encoding, as your numbers should all be basic (ANSI) strings.
To Top