deg2rad

Converts an angle from degrees to its equivalent value in radians.

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

Parameters

ParameterTypeDescription
$numfloatAngular value in degrees.

Return value

floatThe radian equivalent of $num, computed as $num * M_PI / 180.

Errors

Throws a TypeError if $num is not a float or a value that can be coerced to float.

deg2rad() multiplies a degree value by M_PI / 180 to give radians. That is all it does — but it is worth calling rather than inlining, because it reads clearly and uses the full-precision constant every time.

When to reach for it

Every trigonometric function in PHP — sin(), cos(), tan() and friends — works in radians, while humans, map data, compass bearings and CSS all speak degrees. Any time an angle crosses that boundary, convert. The common cases are great-circle distance between two latitude/longitude points, rotating coordinates for a chart or canvas, and computing a heading from a bearing.

What trips people up

Forgetting the conversion is the single most common trigonometry bug in PHP. sin(30) is not 0.5 — it is the sine of 30 radians, about -0.988, and nothing about the result looks obviously wrong, so the error survives review. Write sin(deg2rad(30)).

The result is a float, so equality comparisons are unreliable, though deg2rad(45) === M_PI_4 does happen to hold exactly. Also note the function does not normalise: deg2rad(720) gives roughly 12.566, not 0. Wrap the angle yourself with fmod($deg, 360) first if you need a canonical range.

Example

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

When to use it

  • Compute great-circle distance between two latitude/longitude pairs with the haversine formula.
  • Rotate x/y coordinates by a degree angle when drawing a chart or image.
  • Turn a compass bearing in degrees into the radian input a trig function needs.

Frequently asked questions

Why does sin(30) not return 0.5 in PHP?

PHP's trig functions take radians, so sin(30) computes the sine of 30 radians, roughly -0.988. Use sin(deg2rad(30)) to get 0.5.

What is the difference between deg2rad and rad2deg?

deg2rad() multiplies by M_PI/180 to go from degrees to radians; rad2deg() divides by the same factor to go back. Use deg2rad() on input into trig functions and rad2deg() on output from inverse trig functions like atan2().

Does deg2rad normalise angles to 0-360?

No, it converts the value as given, so deg2rad(720) returns about 12.566 rather than 0. Apply fmod($deg, 360) first if you need a canonical range.

More examples

Haversine distance between two cities

Every angle entering a trig call is converted first, including the latitudes used in the cos() terms. The result is about 343.6 km. Skipping any one of those deg2rad() calls produces a number that still looks like a distance but is badly wrong.

Example · php
<?php
function distanceKm(float $lat1, float $lon1, float $lat2, float $lon2): float {
    $dLat = deg2rad($lat2 - $lat1);
    $dLon = deg2rad($lon2 - $lon1);
    $a = sin($dLat / 2) ** 2
       + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dLon / 2) ** 2;
    return 6371 * 2 * asin(sqrt($a));
}
printf("London to Paris: %.1f km\n", distanceKm(51.5074, -0.1278, 48.8566, 2.3522));

Rotating a point around the origin

The point (1, 0) walks around the unit circle: (1, 0), (0, 1), (-1, 0), (0, -1). The tiny residues you would see without the %6.3f formatting — 6.1e-17 instead of a clean 0 — are normal float error from M_PI not being exact.

Example · php
<?php
$x = 1.0; $y = 0.0;
foreach ([0, 90, 180, 270] as $deg) {
    $r = deg2rad($deg);
    printf("%3d deg -> (%6.3f, %6.3f)\n",
        $deg, $x * cos($r) - $y * sin($r), $x * sin($r) + $y * cos($r));
}

Normalising an angle before converting

deg2rad() does not wrap angles, so 725 degrees becomes 12.6536 radians rather than the equivalent 0.0873. The sines match to six decimals because sin is periodic, but any code comparing raw radian values would treat the two as different angles.

Example · php
<?php
$bearing = 725.0;
printf("raw:        %.4f rad\n", deg2rad($bearing));

$wrapped = fmod($bearing, 360.0);
printf("wrapped:    %.1f deg\n", $wrapped);
printf("as radians: %.4f rad\n", deg2rad($wrapped));
printf("same sine:  %.6f vs %.6f\n", sin(deg2rad($bearing)), sin(deg2rad($wrapped)));

Discussion

  • Be the first to comment on this lesson.