acos() accepts a value from -1 through 1 and returns the corresponding angle in radians.
$angle = acos(0.5);
echo rad2deg($angle); // approximately 60
The examples below build from the basic call to practical variations and edge cases.
<?Php
echo acos(0); // Output is 1.5707963267949
echo "<br>";
echo acos(1); // Output 0
echo "<br>";
echo acos(-1); // Output is 3.1415926535898
echo "<br>";
echo acos(-5); // Output is NAN
?>
Syntax
acos(float $num): float;
The argument is a cosine value, not an angle. Valid real inputs are from -1 through 1. Values outside that range return NAN. Use is_nan() to test the result.
acos() returns radians. Use rad2deg() when the result needs to be displayed in degrees.
$cosine = 0.5;
$angleRadians = acos($cosine);
echo rad2deg($angleRadians); // 60
$value = 2; // Out of range
$result = acos($value);
if (is_nan($result)) {
echo "Invalid input for acos";
}
Output
Invalid input for acos
$a = 5; $b = 6; $c = 7;
$angleC = acos(($a*$a + $b*$b - $c*$c) / (2 * $a * $b));
echo rad2deg($angleC); // Output in degrees
Output
78.463040967185
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.