To convert radians to degrees for use in trigonometric functions in php, the easiest way is with the php rad2deg() function.
$degrees = rad2deg(pi()/2)
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 radians to degrees.
We can convert radians to degrees easily with the php rad2deg() function.
To do so, we need to pass any number to the php rad2deg() function.
Below are a few examples of how to use the rad2deg() function to convert different angles, in radians, to degrees with in php.
echo rad2deg(0);
echo rad2deg(pi()/6);
echo rad2deg(pi()/3);
echo rad2deg(pi()/2);
// Output:
0
30
60
90
If you’d like to go the other way, converting degrees to radians, you can use the deg2rad() php function.
Converting Radians to Degrees Without the rad2deg() function in php
Converting radians to degrees is a very easy formula. To convert radians to degrees, all we need to do is multiply the degrees by 180 and divide by pi.
We can convert radians to degrees without the help of the math module easily in php.
Below is a user-defined function which will convert radians to degrees for us in our php code.
function radians_to_degrees(float $radians) {
return $radians * (180/pi());
}
Let’s test the function to verify that we get the same results as the rad2deg() php function.
function radians_to_degrees(float $radians) {
return $radians * (180/pi());
}
echo radians_to_degrees(0);
echo radians_to_degrees(pi()/6);
echo radians_to_degrees(pi()/3);
echo radians_to_degrees(pi()/2);
// Output:
0
30
60
90
As you can compare for yourself to the example above, we get the same results as using rad2deg()
Hopefully this post was helpful for you to learn how to convert radians to degrees in php with and without the rad2deg() php function.