abs

(PHP 4, PHP 5, PHP 7, PHP 8)

absAbsolute value

Açıklama

abs(int|float $num): int|float

Returns the absolute value of num.

Bağımsız Değişkenler

num

The numeric value to process

Dönen Değerler

The absolute value of num. If the argument num is of type float, the return type is also float, otherwise it is int (as float usually has a bigger value range than int).

Sürüm Bilgisi

Sürüm: Açıklama
8.0.0 num no longer accepts internal objects which support numeric conversion.

Örnekler

Örnek 1 abs() example

<?php
var_dump
(abs(-4.2));
var_dump(abs(5));
var_dump(abs(-5));
?>

Yukarıdaki örneğin çıktısı:

float(4.2)
int(5)
int(5)

Ayrıca Bakınız

add a note add a note

User Contributed Notes 1 note

up
3
eep2004 at ukr dot net
3 years ago
<?php
echo 'PHP '.PHP_VERSION.'<br>';

$qty = 1000;
$arr = array();
for (
$i = 0; $i < $qty; $i++){
   
$arr[] = rand(-100, 100);
}

$start = microtime(true);
for (
$i = 0; $i < $qty; $i++){
    foreach (
$arr as $v){
       
$v = abs($v);
    }
}
echo
number_format(microtime(true) - $start, 4).'<br>';

$start = microtime(true);
for (
$i = 0; $i < $qty; $i++){
    foreach (
$arr as $v){
        if (
$v < 0) $v = abs($v);
    }
}
echo
number_format(microtime(true) - $start, 4).'<br>';

$start = microtime(true);
for (
$i = 0; $i < $qty; $i++){
    foreach (
$arr as $v){
        if (
$v < 0) $v *= -1;
    }
}
echo
number_format(microtime(true) - $start, 4).'<br>';
?>
Result:
PHP 7.1.33
0.0910
0.0710
0.0550

Conclusion: better to check before using the feature that the number is less than zero. Even better use multiplication by -1 than this function.
To Top