rad2deg()
Today, we will discuss the rad2deg() function in PHP. This function is used to convert radians to degrees.
The PHP rad2deg() function converts an angle measured in radians into the equivalent angle in degrees. It is the exact inverse of deg2rad(), and it is handy whenever a math function gives you a result in radians but you need a human-friendly value in degrees.
What Are Radians and Degrees?
Both radians and degrees measure angles — they are just two different units, like meters and feet.
- A full circle is
360degrees, or2πradians. - That makes
1radian equal to180/πdegrees, which is about57.29578degrees.
PHP's built-in trigonometric functions (sin(), cos(), atan(), …) all work in radians, so rad2deg() is the bridge that turns those results back into degrees that most people read.
Syntax
rad2deg(float $num): float$num— the angle in radians you want to convert.- Return value — the same angle expressed in degrees, as a
float.
How to Use the rad2deg() Function
Pass the radian value and rad2deg() returns the angle in degrees:
Here 1.047 radians (close to π/3) is converted to roughly 60 degrees.
Common Reference Values
Using PHP's M_PI constant (the same value returned by pi()), the well-known angles convert like this:
<?php
echo rad2deg(M_PI); // 180
echo "\n";
echo rad2deg(M_PI / 2); // 90
echo "\n";
echo rad2deg(M_PI / 4); // 45
echo "\n";
echo rad2deg(2 * M_PI); // 360
?>rad2deg() vs. deg2rad()
The two functions are mirror images of each other, so converting one way and back returns the original value:
<?php
$degrees = 90;
$radians = deg2rad($degrees); // 1.5707963267949
$back = rad2deg($radians); // 90
echo $back; // 90
?>Use deg2rad() before calling a trig function, and rad2deg() after one.
Practical Example: Reading an Angle From cos()
Inverse trig functions return radians. Convert the result with rad2deg() to get a readable angle, and round() to trim the long float:
<?php
// acos() returns the angle (in radians) whose cosine is 0.5
$radians = acos(0.5);
$degrees = rad2deg($radians);
echo round($degrees, 2); // 60
?>Things to Keep in Mind
rad2deg()always returns afloat, even for whole-number results like180. Useround()ornumber_format()when you need a tidy display value.- It only changes the unit, not the angle itself —
rad2deg(M_PI)andM_PIdescribe the same direction. - Passing a non-numeric string raises a
TypeErrorin PHP 8+; make sure the argument is numeric.
Conclusion
rad2deg() is a small but essential helper for any PHP code that deals with angles. Pair it with deg2rad(), the trig functions, and pi() to move comfortably between the radians the math engine uses and the degrees people understand.