deg2rad()
Today, we will discuss the deg2rad() function in PHP. This function is used to convert an angle from degrees to radians.
The PHP deg2rad() function converts an angle measured in degrees to the equivalent angle in radians. This page explains what it returns, why the conversion matters, how it pairs with PHP's trigonometric functions, and the gotchas to watch for.
Why convert degrees to radians?
People usually think about angles in degrees (a full circle is 360°), but PHP's trigonometric functions — sin(), cos(), tan(), and friends — expect their argument in radians, where a full circle is 2π (about 6.28). If you pass degrees straight into sin(), you get a wrong answer. deg2rad() is the bridge: convert first, then call the trig function.
The math is a simple proportion:
radians = degrees × (π / 180)So deg2rad(180) returns π (≈ 3.14159265…) and deg2rad(90) returns π/2 (≈ 1.5707963…).
Syntax
deg2rad(float $num): float$num— the angle in degrees. Any float or integer is accepted, including negative values and values larger than 360.- Return value — the same angle expressed in radians, as a
float.
deg2rad() is the inverse of rad2deg(), which goes the other direction.
Basic example
This prints:
0.78539816339745That value is π/4 — exactly a quarter of π, which makes sense because 45° is a quarter of 180°.
Using deg2rad() with trigonometric functions
The most common reason to reach for deg2rad() is feeding a degree value into sin(), cos(), or tan():
<?php
// sin() expects radians, so convert the 30° angle first
echo sin(deg2rad(30)); // 0.5
echo "\n";
// Without the conversion the result is meaningless:
echo sin(30); // -0.98803162409286
?>The first line correctly returns 0.5 (the sine of 30°). The second treats 30 as 30 radians — roughly 4.7 full turns around the circle — so the output has nothing to do with 30 degrees. This is the single most frequent mistake when working with angles in PHP.
Working with other angles
deg2rad() accepts any numeric value, not just the "nice" ones:
<?php
echo deg2rad(180); // 3.1415926535898 (π)
echo "\n";
echo deg2rad(360); // 6.2831853071796 (2π)
echo "\n";
echo deg2rad(-90); // -1.5707963267949 (negative angle)
?>For exact values, PHP also exposes pi() and the M_PI constant, so deg2rad(180) and M_PI produce the same number.
Gotchas
- Radians in, radians out of trig functions.
deg2rad()only converts the number; it does not change howsin()/cos()/tan()behave. Always convert before the trig call. - Floating-point precision. Results are floats, so comparisons like
deg2rad(180) == M_PIcan be unreliable due to rounding. Compare with a small tolerance instead of==. echotruncates display.echoshows ~14 significant digits by default; the stored float carries more. Useprintf('%.20f', deg2rad(45))if you need to see the full precision.
Conclusion
deg2rad() converts a degree angle into radians using degrees × π / 180, and it is almost always used right before a trigonometric function such as sin(), cos(), or tan(). Remember that PHP's trig functions work in radians, convert with deg2rad() to go in, and use rad2deg() to go back the other way.