To convert degrees to radians for use in trigonometric functions in php, the easiest way is with the php deg2rad() function.
$radians = deg2rad(60)
The php collection of math functions has many powerful functions which make performing certain calculations in php very easy.
One such calculation which is very easy to perform in php is converting degrees to radians.
We can convert degrees to radians easily with the php deg2rad() function.
To do so, we need to pass any number to the deg2rad() function.
Below are a few examples of how to use the deg2rad() function to convert different angles, in degrees, to radians with in php.
echo deg2rad(0);
echo deg2rad(30);
echo deg2rad(60);
echo deg2rad(90);
// Output:
0
0.5235987755983
1.0471975511966
1.5707963267949
If you’d like to go the other way, converting radians to degrees, you can use the php rad2deg function.
Converting Degrees to Radians Without rad2deg() Function in php
Converting degrees to radians is a very easy formula. To convert degrees to radians, all we need to do is multiply the degrees by pi divided by 180.
We can convert degrees to radians without the help of the math module easily in php.
Below is a user-defined function which will convert degrees to radians for us in our php code.
function degrees_to_radians($degrees):
return $degrees * (pi()/180);
}
Let’s test the function to verify that we get the same results as the deg2rad() php function.
function degrees_to_radians($degrees):
return $degrees * (pi()/180);
}
echo degrees_to_radians(0);
echo degrees_to_radians(30);
echo degrees_to_radians(60);
echo degrees_to_radians(90);
// Output:
0
0.5235987755983
1.0471975511966
1.5707963267949
As you can compare for yourself to the example above, we get the same results as using deg2rad()
Hopefully this post was helpful for you to learn how to convert degrees to radians in php with and without the deg2rad() php function.