Return sum of two number as a Positive Integer

0
416
php sum

you can use the abs() function. For example:

$var1 = -2;
$var2 = -30;

echo abs($var1 - $var2); // 28

You could also define your own function:

function abs_diff($v1, $v2) {
    $diff = $v1 - $v2;
    return $diff < 0 ? (-1) * $diff : $diff;
}

echo abs_diff(-2, -30); // 28

LEAVE A REPLY

Please enter your comment!
Please enter your name here