PHP rad2deg() function


The rad2deg() function in PHP is used to convert an angle from radians to degrees. This function is useful when working with angles that are typically expressed in degrees, but were calculated or measured in radians.

Formula:

The mathematical relationship between radians and degrees is:

degrees=radians×180π\text{degrees} = \text{radians} \times \frac{180}{\pi}

Where π3.1415926535898\pi \approx 3.1415926535898.

Syntax:

rad2deg($radians)

Parameters:

  • $radians: The angle in radians that you want to convert to degrees.

Return Value:

  • The function returns the equivalent value in degrees as a float.

Example 1: Converting Radians to Degrees

<?php $radians = 3.1415926535898; // Convert radians to degrees $degrees = rad2deg($radians); echo $degrees; ?>

Output:

180

In this example, π\pi radians is equivalent to 180 degrees.

Example 2: Converting π2\frac{\pi}{2} Radians

<?php $radians = pi() / 2; // Convert radians to degrees $degrees = rad2deg($radians); echo $degrees; ?>

Output:

90

Here, π2\frac{\pi}{2} radians is equivalent to 90 degrees.

Practical Usage:

  • The rad2deg() function is helpful when working with angles in radians that need to be converted to degrees for readability, user interfaces, or situations where degrees are more commonly used.
  • It is often paired with trigonometric functions (such as sin(), cos(), tan()) which may return angles in radians, and then you can convert those to degrees for easier understanding.

Example 3: Using rad2deg() After a Trigonometric Calculation

<?php $angle_radians = asin(0.5); // Calculate the arc sine of 0.5, which is in radians // Convert radians to degrees $angle_degrees = rad2deg($angle_radians); echo "The angle is: " . $angle_degrees . " degrees"; ?>

Output:

The angle is: 30 degrees

Summary:

  • rad2deg() converts an angle from radians to degrees.
  • It’s useful in situations where angles calculated in radians (such as those from trigonometric functions) need to be converted to degrees for interpretation or display.