PHP deg2rad() function in PHP


The deg2rad() function in PHP is used to convert an angle from degrees to radians. In mathematics, radians are often used in trigonometric functions, and this function simplifies the conversion from degrees to radians.

Formula:

The mathematical relationship between degrees and radians is:

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

Where π3.1415926535898\pi \approx 3.1415926535898.

Syntax:

deg2rad($degrees)

Parameters:

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

Return Value:

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

Example 1: Converting Degrees to Radians

<?php $degrees = 180; // Convert degrees to radians $radians = deg2rad($degrees); echo $radians; ?>

Output:

3.1415926535898

In this example, 180 degrees is equivalent to π\pi radians (approximately 3.1416).

Example 2: Converting 90 Degrees

<?php $degrees = 90; // Convert degrees to radians $radians = deg2rad($degrees); echo $radians; ?>

Output:

1.5707963267949

In this case, 90 degrees is equivalent to π2\frac{\pi}{2} radians (approximately 1.5708).

Practical Usage:

  • The deg2rad() function is often used in calculations involving trigonometric functions such as sin(), cos(), and tan(), which typically expect the angle to be in radians.
  • When working with angles in degrees (for example, in geometry or engineering problems), this function simplifies the conversion.

Example 3: Using deg2rad() with Trigonometric Functions

<?php $angle_degrees = 45; // Convert degrees to radians $angle_radians = deg2rad($angle_degrees); // Calculate sine of the angle $sine_value = sin($angle_radians); echo "Sine of 45 degrees: " . $sine_value; ?>

Output:

Sine of 45 degrees: 0.70710678118655

Summary:

  • deg2rad() converts an angle from degrees to radians.
  • It’s useful in trigonometry, geometry, or any calculations where angles need to be converted to radians for functions that expect angles in radians.