Mathematics7 min readExamples run on PHP 8.5.10

deg2rad()

Learn how PHP 8.4 deg2rad() converts angles from degrees to radians, with tested examples, edge cases and the mistakes to avoid in trigonometry code.

Signature

deg2rad(float $num): float

Purpose

deg2rad() converts an angle measured in degrees into radians. People think in degrees: a right angle is 90, a half turn is 180. PHP's trigonometric functions such as sin(), cos() and tan() expect radians. deg2rad() bridges the two, so your code can keep human-friendly values and convert them just before calling the math functions.

One full turn is 360 degrees, which equals 2π radians. The conversion is therefore degrees multiplied by π and divided by 180.

Signature

Parameters and defaults

  • $num: the angle in degrees. It is required and has no default.

Because the parameter is typed float, an integer such as 90 is converted to a float automatically. A numeric string such as "90" is also accepted in the default (non-strict) typing mode. Under declare(strict_types=1), only int and float values are accepted.

Return value and failure cases

The function returns the radian equivalent as a float. There is no error return value such as false. The only failure you will normally meet is a TypeError, which PHP throws when the argument cannot be used as a float, for example an array or a non-numeric string like "abc".

The result is not normalized. An input of 720 gives roughly 4π, not 0, so the function never wraps an angle into a single turn.

Example 1: common angles

The results are floats with many digits, so the examples format them with number_format() to get stable output.

Example
<?php
echo number_format(deg2rad(180), 5), PHP_EOL;
echo number_format(deg2rad(90), 5), PHP_EOL;
echo number_format(deg2rad(-45), 5), PHP_EOL;
Output
3.14159
1.57080
-0.78540

A half turn is π (3.14159), a quarter turn is π/2 (1.57080), and a negative angle gives a negative radian value.

Example 2: exactness and no wrapping

Some conversions land exactly on a PHP constant. 45 degrees equals M_PI_4, and the strict comparison holds. Larger angles are simply converted without being reduced.

Example
<?php
var_dump(deg2rad(45) === M_PI_4);
echo number_format(deg2rad(720), 4), PHP_EOL;
Output
bool(true)
12.5664

Do not assume every conversion compares equal to a constant. Floating-point results are approximations, so use a tolerance when comparing other values.

Example 3: polar to Cartesian coordinates

A typical use is placing a point on a circle. You have a radius and an angle in degrees, and you need x and y.

Example
<?php
function polarToCartesian(float $radius, float $degrees): array
{
    $radians = deg2rad($degrees);
    return [$radius * cos($radians), $radius * sin($radians)];
}

[$x, $y] = polarToCartesian(10, 30);
echo number_format($x, 3), ', ', number_format($y, 3), PHP_EOL;
Output
8.660, 5.000

The point at 30 degrees on a circle of radius 10 is about (8.660, 5.000).

Edge cases

  • Floating-point noise: sin(deg2rad(180)) is not exactly 0. It is a tiny number close to zero. Round or format the result before displaying it or comparing it.
  • Negative and large angles: these are converted proportionally. Normalize them yourself with a modulo approach if you need a value in one turn.
  • Integer input: an int is accepted and converted to float, and the result is always a float.
  • Precision: a float has limited precision, so very large degree values lose fine detail.

Common mistakes

  1. Passing degrees straight to sin() or cos(). sin(90) does not give 1, because PHP reads 90 as radians. Convert first with deg2rad().
  2. Converting twice. If a value is already in radians, calling deg2rad() shrinks it by a factor of about 57.3.
  3. Comparing floats with == or ===. Results like cos(deg2rad(90)) are close to, not exactly, zero. Use abs($a - $b) < 1e-9 or round first.
  4. Echoing raw floats in tests. The number of digits shown depends on configuration, so format with number_format(), round() or json_encode() for predictable output.
  5. Expecting wrap-around. deg2rad(360) is 2π, not 0.
  • rad2deg() performs the inverse conversion, from radians to degrees.
  • sin(), cos() and tan() consume radian values.
  • atan2() returns an angle in radians, which you can convert back with rad2deg().
  • M_PI and M_PI_4 are constants that express common radian values directly.
  • round() and number_format() help present trigonometric results.

Recap

deg2rad(float $num): float turns degrees into radians. It returns a float, does not normalize the angle, and throws a TypeError for unusable input. Use it right before calling trigonometric functions, format or round the output, and never compare the results with exact equality unless you know the value is exact.

Tested examples

Converting common angles
<?php
echo number_format(deg2rad(180), 5), PHP_EOL;
echo number_format(deg2rad(90), 5), PHP_EOL;
echo number_format(deg2rad(-45), 5), PHP_EOL;
Output
3.14159
1.57080
-0.78540
Exact match with M_PI_4 and no wrapping
<?php
var_dump(deg2rad(45) === M_PI_4);
echo number_format(deg2rad(720), 4), PHP_EOL;
Output
bool(true)
12.5664
Polar to Cartesian coordinates
<?php
function polarToCartesian(float $radius, float $degrees): array
{
    $radians = deg2rad($degrees);
    return [$radius * cos($radians), $radius * sin($radians)];
}

[$x, $y] = polarToCartesian(10, 30);
echo number_format($x, 3), ', ', number_format($y, 3), PHP_EOL;
Output
8.660, 5.000