abs

(PHP 3, PHP 4, PHP 5)

abs -- 绝对值

说明

number abs ( mixed number )

返回参数 number 的绝对值。如果参数 numberfloat,则返回的类型也是 float,否则返回 integer(因为 float 通常比 integer 有更大的取值范围)。

例子 1. abs()

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

add a note add a note User Contributed Notes
Josh
08-Jan-2006 08: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;
}

?>
jeremys at hang dash wire dot com
13-Dec-2005 12:05
I'm unable to replicate concordia's problem with the $n = $n - $n * 2 code.  I agree with the simplification to $n *= -1.  But there's no reason that concordia's code should return 6 for the value -2, and it doesn't appear to.  When I tried it, PHP returned 2, as it should.  If PHP were somehow flipping the sign of integers randomly, that would be a *major* bug!

There doesn't seem to be a sgn() function yet.  Here's some quick code to do it:

function sgn($x) {
   return $x ? ($x>0 ? 1 : -1) : 0;
}

You could use $x ? abs($x)/$x : 0 too, but might as well avoid the float division.
Lazarus
12-Dec-2005 10:53
Even that is unnecessarily complex. Try this:

<?php

function n_abs($v) { return -abs($v) ; }

?>

Faster too.
rdk
11-Nov-2005 03:04
concordia, you seem to be overcomplicating matters.  If you want to do the reverse of the abs function, the only code required is:

<?php

function n_abs($num) {
   return (
$num > 0) ? $num * -1 : $num;
}

n_abs(2); //-2
n_abs(-2); //-2

?>

If you want to switch the sign bit of a number, as your example seems to indicate...
turn_neg (2); // -2
turn_neg (-2); // 2
...you just need to do $num *= -1;

Your function also doesn't seem to work. Switching "if (is_neg ($num))" to "if (!is_neg ($num))" would make it function as indicated by your comments (i.e. toggle the sign bit), but it would still be overly complicated.
concordia at game dot xakep dot org
07-Nov-2005 05:35
Sometimes you may want to do the opposite of abs(): turn a positive number into a negative.

<?php

function turn_neg ($num) {
   return
$num - $num * 2;
}

?>

But this can create errors when you put a negative number inside...

turn_neg (-2) returns 6.
 
<?php

turn_neg
(-2); // 6.

?>

The solution is to make another function to determine if the number is negative or not.

<?php

function is_neg ($num) {
   return
$num < 0;
}

function
turn_neg ($num) {
   if (
is_neg ($num)) {
       return
$num - $num * 2;
   } else {
       return
abs ($num);
   }
}

turn_neg (2); // -2
turn_neg (-2); // 2

?>

Or, if the number is not negative, you could also return false.
alreece45 at yahoo dot com
11-Aug-2005 03:05
Both of the below codes were wrong.

<?php

// Check to see if $range is numberic, if not, set it to the integer value 1

if(!is_numeric($range)) {

 
$range=1;

}

// Check to see if $range is an integer and not a float. Use is_int() and not int().

if(!is_int($range)) {

// Make it an integer. Use intval() here, not int_val.

 
$range=intval($range);

}

$range=abs($range);

?>

The only thing I don't get is why we have to check before doing the functions. Whether or not we do the functions it will give us what we want. The only check I really understand being there is the one that sets it to one.

Why not just do:

<?php

// If $range is numberic, make it in positive integer, otherwise, make it one.

$range = is_numeric($range) ? abs(intval($range)) : 1;

?>

Seems like a lot less code. Or If you prefer to stay with if statements:

<?php

if(is_numberic($range)) {

 
// If $range is numberic, make it in positive integer.

 
$range = abs(intval($range));

}

else {

 
//otherwise, make it one.

 
$range = 1;

}

?>

Both ways seem smaller to me.
abodeman at y a h o o
25-May-2004 08:59
There's another problem in the below code. The correct function to see whether a value is an integer is is_int(), not int(), so the code should look like this:

<?php
if(!is_numeric($range))//checks for numeric input
{
$range=1;
}
//sets $range to integer 1 if input is anything other than a numeric value
if(!is_int($range))//checks to make sure it is an integer (not decimal)
{
$range=int_val($range);
}
//if a decimal- sets $range to integer value
$range=abs($range);//sets value to positive whole number
?>
mbender at duforu dot com
02-Mar-2004 05:59
In reference to the previous code sample the int_val function is actually intval() [http://us4.php.net/manual/en/function.intval.php]:

<?php
if(!is_numeric($range))//checks for numeric input
 
{
 
$range=1;
 }
//sets $range to integer 1 if input is anything other than a numeric value
if(!int($range))//checks to make sure it is an integer (not decimal)
 
{
 
$range=intval($range) // not ->  int_val($range);
 
}//if a decimal- sets $range to integer value
$range=abs($range);//sets value to positive whole number
?>
bgustin AT trukfixer DOT com
16-Jul-2003 11:50
assume we take user input from a form untreated and assign it to variable $range. We want to be sure this number is a Positive Whole number, since abs() just sets a number to positive or 0, and we dont want decimals...

<?php
if(!is_numeric($range))//checks for numeric input
{
$range=1;
}
//sets $range to integer 1 if input is anything other than a numeric value
if(!int($range))//checks to make sure it is an integer (not decimal)
{
$range=int_val($range);
}
//if a decimal- sets $range to integer value
$range=abs($range);//sets value to positive whole number
?>

for example the input "testing" would return $range =1,
the input "3.578" would return value=3
If the input is null, I am havent tested to see if it gets set to 1 courtesy of int_val or not, but I believe it will be.

I'm sure there's probably a more elegant way to do this using regex, however for an apprentice php coder, this might be a little easier to understand and use.