PHP abs() Function

In this chapter you will learn:

  1. Definition for PHP abs() Function
  2. Syntax for PHP abs() Function
  3. Parameter for PHP abs() Function
  4. Return for PHP abs() Function
  5. Example - Get the absolute value

Definition

The abs() function returns the absolute value of a number.

Syntax

PHP abs() Function has the following syntax.

abs(number);

Parameter

ParameterDescription
numberRequired. A number.

Return

PHP abs() function returns the absolute value of the number.

If the number is of type float, the return type is also float, otherwise it is integer.

Example

You can either send a floating-point number or an integer to abs(), and it will return the same type:


<?PHP/* j  a v  a  2 s . c o m*/
    echo abs(50); // 50 
    echo "\n";
    echo abs(-12); // 12 
    echo "\n";
    echo abs(50.1); // 50.1 
    echo "\n";
    echo abs(-12.5); // 12.5 
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP acos() Function
  2. Syntax for PHP acos() Function
  3. Parameter for PHP acos() Function
  4. Return for PHP acos() Function
  5. Example - Calculate the arc cosine value