cos()
Today, we will discuss the cos() function in PHP. This function is used to get the cosine of an angle.
The cos() function in PHP returns the cosine of an angle. It is one of PHP's built-in trigonometric functions and is essential whenever you work with angles, waves, rotations, or geometry. This page covers the syntax, the all-important radians requirement, the return range, worked examples, and the gotchas that trip people up.
Syntax
cos(float $num): float$num— the angle, expressed in radians (not degrees).- Return value — the cosine of
$num, always afloatin the range -1 to 1 inclusive.
Radians, not degrees
This is the single most common mistake with cos(). PHP expects the angle in radians, while people usually think in degrees. Passing cos(45) does not give you the cosine of 45 degrees — it treats 45 as 45 radians and returns something unexpected.
To convert degrees to radians, use deg2rad(). The relationship is:
radians = degrees × (π / 180)Basic example
Here we start with 45 degrees, convert it to radians with deg2rad(), then pass the result to cos(). The output is approximately 0.70710678118655 — that is √2 / 2, the exact cosine of 45 degrees.
Common angles
The table below shows the values cos() returns for a few familiar angles. Note that cos(M_PI / 2) does not return exactly 0: it returns a tiny number like 6.1232339957368E-17. That is normal floating-point rounding — π cannot be represented exactly, so the result is "very close to zero" rather than a clean zero.
<?php
echo cos(0), "\n"; // 1
echo cos(M_PI), "\n"; // -1
echo cos(M_PI / 2), "\n"; // 6.1232339957368E-17 (≈ 0)
echo cos(deg2rad(60)), "\n"; // 0.5
?>M_PI is a built-in PHP constant for the value of π. See pi() for the function form.
When would I use cos()?
cos() shows up far beyond a math classroom:
- Graphics and games — rotating a point around an origin, or placing items evenly around a circle.
- Animation — driving smooth, looping motion (a value that oscillates between -1 and 1).
- Physics and signal work — modeling waves and periodic behaviour.
Placing points around a circle
A practical use of cosine is computing the X coordinate of a point on a circle of a given radius:
<?php
$radius = 100;
for ($deg = 0; $deg < 360; $deg += 90) {
$x = $radius * cos(deg2rad($deg));
echo "At {$deg}°, x = " . round($x, 2) . "\n";
}
?>This prints 100, 0, -100, and 0 (the X positions at 0°, 90°, 180°, and 270°), with the 90° and 270° values being effectively zero apart from floating-point dust.
Related functions
sin()— the sine of an angle.tan()— the tangent of an angle.acos()— the inverse: given a cosine, return the angle.deg2rad()— convert degrees to radians before callingcos().
Conclusion
cos() returns the cosine of an angle given in radians, always producing a float between -1 and 1. The key things to remember are to convert degrees with deg2rad() first, and to expect tiny floating-point residue instead of perfect zeros for angles like 90°. With those in mind, cos() is a reliable building block for graphics, animation, and any periodic or geometric calculation.