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

search for in the

acos> <Mathematische Funktionen
[edit] Last updated: Fri, 17 May 2013

view this page in

abs

(PHP 4, PHP 5)

absAbsolutwert bzw. Betrag

Beschreibung

number abs ( mixed $number )

Liefert den Betrag des Wertes number ohne Vorzeichen.

Parameter-Liste

number

Der zu bearbeitende Wert

Rückgabewerte

Der Betrag des Wertes number. Ist number vom Typ float, so ist auch das Ergebnis von diesem Typ, ansonsten wird das Ergebnis als integer zurückgegeben (da float einen größeren Wertebereich als integer hat).

Beispiele

Beispiel #1 abs()-Beispiel

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

Siehe auch



add a note add a note User Contributed Notes abs - [2 notes]
up
-1
Ister
4 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
-2
svein dot tjonndal at gmail dot com
1 year 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.

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