What is cos() in PHP?

The cos() function in PHP calculates the cosine of a number in radians.

The following illustration shows the mathematical representation of the cos() function.

Mathematical representation of the cosine function

Note: The cos() function only works for right-angled triangles.

Syntax

float cos(num)

Parameter

This function requires a number that represents an angle in radians as a parameter.

To convert degrees to radians, use the following formula.

radians = degrees * ( M_PI / 180.0 )

Return value

cos() returns the cosine of a number that is sent as a parameter. The return value is in the range [-1,1].

Code

<?php
#Positive number in radians
echo("cos(2.3): ");
echo (cos(2.3));
?>
<?php
#Negative number in radians
echo("cos(-2.3): ");
echo (cos(-2.3));
?>
<?php
#converting the degrees angle into radians and then applying cos()
#degrees = 180
echo("cos(180 * (M_PI / (180))): ");
echo (cos(180 * (M_PI / (180))));
?>

Free Resources

Attributions:
  1. undefined by undefined