Signature
rad2deg(float $num): floatPurpose
PHP's trigonometric functions such as sin(), cos(), tan() and atan2() work in radians. People, however, usually think and talk in degrees: a compass bearing, a rotation in a drawing tool, or a slope angle. rad2deg() bridges the gap by converting an angle measured in radians into the equivalent angle in degrees. A half turn is M_PI radians and 180 degrees, so the conversion is proportional: degrees equal radians divided by pi, times 180.
Signature
The function has been available since PHP 4 and its PHP 8.4 signature is a single float parameter returning a float.
Parameters and defaults
$num: an angle in radians. There are no other parameters and no default values.
Because the parameter is declared float, an integer such as 0 or 2 is accepted and widened to a float. In the default (weak typing) mode, a numeric string such as "1.5" is also coerced. Under declare(strict_types=1), only int and float values are accepted.
Return value and failure cases
The function returns the angle in degrees as a float. It never returns false and does not emit warnings for ordinary numbers. The input is not normalized: 2 * M_PI gives roughly 360, and a negative input gives a negative result. Passing a value that cannot be treated as a float, for example a non-numeric string like "abc" or an array, throws a TypeError. Special float values pass through the arithmetic naturally: an infinite input produces an infinite result and NAN stays NAN.
Example 1: the manual's basic case
A quarter of a half-turn, M_PI_4, is 45 degrees.
echo rad2deg(M_PI_4), "\n";45
Example 2: converting a list of angles
Floating-point arithmetic can leave tiny errors in the last digits, so it is good practice to round when you display or compare results. Here every quarter turn is converted and rounded to six decimals.
$radians = [0, M_PI_2, M_PI, 3 * M_PI_2];
$degrees = array_map(fn(float $r): float => round(rad2deg($r), 6), $radians);
echo implode(", ", $degrees), "\n";0, 90, 180, 270
Example 3: a compass-style angle from coordinates
atan2($y, $x) returns an angle in radians between -pi and pi. Converting it with rad2deg() gives -180 to 180, and adding 360 to negative results maps the angle into the 0 to 360 range.
function angleOf(float $x, float $y): float
{
$deg = rad2deg(atan2($y, $x));
if ($deg < 0) {
$deg += 360;
}
return round($deg, 6);
}
$points = [[1, 1], [-1, 1], [-1, -1], [1, -1]];
$angles = array_map(fn(array $p): float => angleOf($p[0], $p[1]), $points);
echo implode(", ", $angles), "\n";45, 135, 225, 315
Edge cases
- Floating-point noise. Values that are mathematically exact may differ from the expected number in the final digits. Round for display, and compare with a tolerance instead of
==. - No wrapping. An input of
4 * M_PIyields about 720. If you need a value in a single turn, normalize it yourself, for example by usingfmod()and adjusting negative results. - Negative angles. The sign is preserved, so
-M_PI_2converts to about -90. - Huge inputs. Very large radian values convert to very large degree values; precision of the result is limited by the float type.
- Infinity and NAN. These propagate rather than raising errors, so check with
is_finite()oris_nan()if your data might contain them.
Common mistakes
- Passing degrees to rad2deg(). The function expects radians. If you already have degrees, use
deg2rad()when you need to feedsin()orcos(), and do not convert twice. - Converting the wrong direction. Use
deg2rad()before trigonometric functions andrad2deg()after inverse ones such asasin(),acos(),atan()andatan2(). - Comparing results strictly. Writing
rad2deg($x) === 90.0can fail because of rounding error. Round first or use a small epsilon. - Assuming a 0 to 360 range. The result mirrors the input.
atan2()gives negative angles for points below the x-axis, so normalize when you want a bearing. - Feeding in unvalidated text. Form data such as
"abc"causes aTypeError. Validate or cast input with a numeric check first. - Relying on echo for precision.
echoprints floats using theprecisionsetting, which can hide or reveal digits. Format deliberately withround()ornumber_format().
Related functions
deg2rad(): the inverse conversion, degrees to radians.sin(),cos(),tan(): trigonometric functions that take radians.asin(),acos(),atan(),atan2(): inverse functions that return radians.pi()and theM_PIfamily of constants: common radian values.round()andfmod(): useful for cleaning up and normalizing results.
Recap
rad2deg() takes one float in radians and returns the same angle in degrees as a float. It does not normalize, does not return false, and throws a TypeError only for non-numeric types. Expect small floating-point differences, round before displaying, and pair it with atan2() and friends, which produce radians.
Tested examples
echo rad2deg(M_PI_4), "\n";45
$radians = [0, M_PI_2, M_PI, 3 * M_PI_2];
$degrees = array_map(fn(float $r): float => round(rad2deg($r), 6), $radians);
echo implode(", ", $degrees), "\n";0, 90, 180, 270
function angleOf(float $x, float $y): float
{
$deg = rad2deg(atan2($y, $x));
if ($deg < 0) {
$deg += 360;
}
return round($deg, 6);
}
$points = [[1, 1], [-1, 1], [-1, -1], [1, -1]];
$angles = array_map(fn(array $p): float => angleOf($p[0], $p[1]), $points);
echo implode(", ", $angles), "\n";45, 135, 225, 315