PHP cos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP cos() function returns the cosine of an angle, which is a number between -1 and 1.

The angle must be given in radians, not degrees, so use deg2rad() first when you start with degrees.

Syntax

Call cos() with the values you need. Start with this line.

PHP
$result = cos($num);
CallWhat it does
cos(0)Returns 1.
cos(M_PI)Returns -1.
cos(deg2rad(60))Returns about 0.5.

The input is in radians. Multiply degrees by pi / 180 or use deg2rad().

Library declarations
PHP
cos(float $num): float

Parameters

ParameterDescription
numRequired. The angle in radians.

Return Value

Return ValueDescription
floatThe cosine of $num, a value from -1 to 1.

PHP Examples

Example 1 — Basic cos() usage

This example finds the cosine of a few common angles in radians.

PHP
<?php
echo cos(0) . PHP_EOL;
echo cos(M_PI) . PHP_EOL;
echo round(cos(M_PI / 3), 4) . PHP_EOL;
echo round(cos(1), 4) . PHP_EOL;

How It Works

  1. cos(0) is exactly 1.
  2. cos(M_PI) is -1 because pi radians is 180 degrees.
  3. M_PI / 3 is 60 degrees, and its cosine is 0.5.
  4. cos(1) is about 0.5403, where 1 means 1 radian, not 1 degree.

Example 2 — Tiny floating point errors

The cosine of 90 degrees should be 0, but computers store pi as a close approximation, so a very tiny number appears.

PHP
<?php
$value = cos(M_PI / 2);

echo $value . PHP_EOL;
echo round($value, 10) . PHP_EOL;
var_dump(abs($value) < 0.0000001);

How It Works

  1. M_PI / 2 is 90 degrees in radians, but pi cannot be stored perfectly.
  2. That is why cos() gives 6.1232339957368E-17 instead of exactly 0.
  3. round($value, 10) removes the tiny error and prints 0.
  4. Comparing abs($value) with a small number is the safe way to test for zero.

Example 3 — Cosine of angles in degrees

Most people think in degrees, so we convert them with deg2rad() before calling cos().

PHP
<?php
$angles = [0, 60, 90, 180];

foreach ($angles as $deg) {
    $value = round(cos(deg2rad($deg)), 4);
    echo "cos($deg degrees) = $value" . PHP_EOL;
}

How It Works

  1. The $angles array holds four angles in degrees.
  2. deg2rad($deg) converts each angle to radians.
  3. cos() returns the cosine and round(..., 4) hides tiny errors.
  4. The output matches the well-known cosine values for 0, 60, 90 and 180 degrees.

Frequently Asked Questions

cos() returns the cosine of an angle given in radians. The result is always between -1 and 1.
Radians. Use deg2rad() to convert degrees first, for example cos(deg2rad(60)).
Pi cannot be stored exactly in a float, so there is a tiny error. Use round() or compare with a small tolerance.
The inverse function is acos(), which takes a cosine value and returns the angle in radians.
Always from -1 to 1, no matter how large the angle is.

Next: cosh()

cosh() function returns the hyperbolic cosine of a number.

cosh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful