PHP acos() Function

Quick summary: The PHP acos() function returns the arc cosine of a number in radians.

PHP acos() Syntax

acos(float $num): float
PHP

PHP acos() Basic examples

echo acos(1);
PHP
Output:
0

The arccosine of 1 is 0 radians.

echo acos(0);
PHP
Output:
1.5707963267949

Returns the angle in radians.

PHP acos() Real-world usage

$adjacent = 5;
$hypotenuse = 10;
$angle = acos($adjacent / $hypotenuse);
echo $angle;
PHP
Output:
1.0471975511966

Calculates an angle using cosine ratio.

PHP acos() Edge cases

var_dump(acos(2));
PHP
Output:
float(NAN)

Input must be between -1 and 1. Otherwise, result is NAN.

PHP acos() Common mistakes

Using degrees instead of radians

acos() returns radians, not degrees.

Incorrect
echo acos(0.5);
Correct
echo rad2deg(acos(0.5));

Convert radians to degrees if needed.

PHP acos() Frequently Asked Questions

What does acos() do?

Returns arc cosine of a number.

Input range?

Between -1 and 1.

Return unit?

Radians.

Use case?

Trigonometric calculations.

What if value out of range?

Returns NAN.

Common mistake?

Using degrees instead of radians.

Performance?

Fast.

Convert to degrees?

Use rad2deg().

Safe?

Yes.

Return type?

float.

Alternative?

Math libraries.

Used in geometry?

Yes.

PHP acos() Related PHP Functions