rad2deg

Converts an angle expressed in radians to its equivalent in degrees.

Signature
rad2deg(float $num): float
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$numfloatA radian value to convert.

Return value

floatThe equivalent of num in degrees.

Errors

Throws a TypeError if num is not a number and cannot be converted to a float.

Every trigonometric function in PHP — sin(), cos(), tan(), atan2(), asin() — speaks radians. Humans, map APIs and CSS transforms mostly speak degrees. rad2deg() is the bridge on the way out, and deg2rad() is the bridge on the way in.

The arithmetic is simply $num * 180 / M_PI, so you could inline it. The function exists because it reads better and because it removes the chance of writing the ratio upside-down.

Where it actually matters

The most common real use is turning the output of atan2() into a compass bearing or a rotation angle. atan2() returns a value between -M_PI and M_PI, so after conversion you get a range of -180 to 180 degrees — not 0 to 360. If you need a positive bearing, normalise it yourself with fmod($deg + 360, 360).

No rounding happens

rad2deg() returns a raw float. rad2deg(M_PI) prints as 180, but half a rotation built up from many small floating-point steps can land on 179.99999999999997. Round explicitly before displaying or comparing angles.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Turn an atan2() result into a compass bearing between two GPS coordinates
  • Convert a physics simulation's radian rotation into a CSS transform in degrees
  • Display the angle of a chart segment computed from a proportion of 2 * M_PI

Frequently asked questions

What is the difference between rad2deg and deg2rad?

They are exact inverses: rad2deg() takes radians and returns degrees, deg2rad() takes degrees and returns radians. PHP's trigonometric functions all use radians, so you call deg2rad() on the way in and rad2deg() on the way out.

Does rad2deg keep the angle between 0 and 360?

No. It multiplies by 180/M_PI and nothing else, so rad2deg(3 * M_PI) returns 540 and negative radians produce negative degrees. Wrap the result yourself with fmod($deg + 360, 360) if you need a single turn.

Why does rad2deg return 179.99999999999997 instead of 180?

Because the radian input was itself the result of floating-point arithmetic that did not land exactly on M_PI. Round the output with round($deg, 6) before displaying or comparing angles.

More examples

Round trip with deg2rad()

Each degree value is converted to radians and straight back. The final column reproduces the input exactly, confirming the two functions are exact inverses within float precision.

Example · php
<?php
foreach ([0, 30, 45, 90, 180, 270] as $deg) {
    $rad = deg2rad($deg);
    printf("%3d deg = %.6f rad -> %.1f deg\n", $deg, $rad, rad2deg($rad));
}

Compass bearing between two points

atan2() returns radians in the range -PI to PI, so rad2deg() alone would give a negative bearing. The fmod(... + 360, 360) step normalises it into the 0-360 range a compass expects.

Example · php
<?php
$lat1 = deg2rad(51.5074); $lon1 = deg2rad(-0.1278); // London
$lat2 = deg2rad(48.8566); $lon2 = deg2rad(2.3522);  // Paris

$y = sin($lon2 - $lon1) * cos($lat2);
$x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($lon2 - $lon1);

$bearing = fmod(rad2deg(atan2($y, $x)) + 360, 360);
echo round($bearing, 1), " degrees\n";  // 148.1 degrees

Angles are not wrapped automatically

rad2deg() is a pure multiplication, so 3 * M_PI radians converts to 540 degrees rather than wrapping to 180. Use fmod() to fold values back into a 0-360 range, adding 360 first to handle negatives.

Example · php
<?php
echo rad2deg(3 * M_PI), "\n";   // 540
echo rad2deg(-M_PI_2), "\n";    // -90

// Wrap into a single turn yourself
$deg = rad2deg(3 * M_PI);
echo fmod($deg, 360), "\n";                 // 180
echo fmod(fmod(-90, 360) + 360, 360), "\n"; // 270

Discussion

  • Be the first to comment on this lesson.