PHP abs - Syntax
abs(mixed $number): mixed
The abs() function in PHP is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
abs(mixed $number): mixed
mixed $number |
Specifies the real number (int, long, float, double) / infinity (*Required) |
---|---|
Return mixed |
Returns a number / infinity respective to an input argument. |
Convert the positive and negative integer to an absolute value.
<?php
class MathExample {
public function getAbsoluteValue() {
// Positive integer value
$n = abs(9);
echo("The absolute value of 9 is " . $n . "\n");
// Negative integer value
$x = abs(-6);
echo("The absolute value of -6 is " . $x . "\n");
}
}
$obj = new MathExample();
$obj->getAbsoluteValue();
Convert the positive and negative float to an absolute value.
<?php
class MathExample {
public function getAbsoluteValue() {
// Positive floating point value
$n = abs(9.3);
echo("The absolute value of 9.3 is " . $n . "\n");
// Negative floating point value
$x = abs(-6.3);
echo("The absolute value of -6.3 is " . $x . "\n");
}
}
$obj = new MathExample();
$obj->getAbsoluteValue();
Convert the positive and negative Infinity to an absolute value.
<?php
class MathExample {
public function getAbsoluteValue() {
// Positive ∞ infinite value
$n = abs(INF);
echo("The absolute value of INF is " . $n . "\n");
// Negative ∞ infinite value
$x = abs(-INF);
echo("The absolute value of -INF is " . $x . "\n");
}
}
$obj = new MathExample();
$obj->getAbsoluteValue();