downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

acos> <Math Funkcje
[edit] Last updated: Fri, 23 Mar 2012

view this page in

abs

(PHP 4, PHP 5)

absWartość bezwględna (moduł z liczby)

Opis

number abs ( mixed $liczba )

Zwraca wartość bezwględna liczby.

Parametry

liczba

Liczba do przetworzenia

Zwracane wartości

Wartość bezwzględną (moduł) podanego argumentu liczba. Jeśli argument liczba jest typu float, zwracana jest także wartość float. W innych przypadkach zwracana jest wartość integer (float ma zwykle szerszy zakres niż integer).

Przykłady

Przykład #1 Przykład abs()

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

Zobacz też:



acos> <Math Funkcje
[edit] Last updated: Fri, 23 Mar 2012
 
add a note add a note User Contributed Notes abs
svein dot tjonndal at gmail dot com 25-May-2011 03:44
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.
Ister 17-Jul-2008 04:59
[*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.
Josh 07-Jan-2006 06:06
Let's say you are resizing images to a standard size that can be expressed as a ratio (width/height). The problem I came into was that I wanted to be reasonable with the proportion of the images that my customer is uploading (couldn't we all use a little less horizontal on pictures?), but I wanted to reject the horizontal pictures when they were uploading vertical ones. So I wanted to accept proportions of images that were within a reasonable threshold (+ or -) of what I will be resizing them to.

Assuming a standard of 1 to 4 (0.25) and a threshold of no more than 0.05 deviation, then the number 0.30 and 0.20 would return true and 0.19 would return false.

<?php

function threshold($given,$thresh,$standard)
{
     return (
abs($given-$standard)<=$thresh) ? true : false;
}

?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites