PHP rad2deg() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

Most trigonometric functions like sin, cos, tan etc require angle argument in radians. Whereas, in practice angle is represented in degrees. The rad2deg() function proves useful in conversion of radian angle to degrees.

This function returns a float number such that number=rad2deg(x) where x is angle in radians. angle in radian = angle in deg *180/pi

For example rad2deg(Π) is equal to 180 degrees

Syntax

rad2deg ( float $number ) : float

Parameters

Sr.NoParameter & Description
1number
A float number reprsenting angle in radians

Return Values

PHP rad2deg() function returns a float number that represents angle in degrees.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example calculates radian equivalent of Π radians −

<?php
   $angle=M_PI;
   echo "angle in degrees = " . rad2deg($angle);
?>

Output

This will produce following result −

rad2deg(3.1415926535898) = 180

Example

 Live Demo

PHP constant M_PI_4 is equal to 45 degrees. We can confirm the result by following example. −

<?php
   $angle=M_PI_4;
   echo "rad2deg(" . $angle . ") = " . rad2deg($angle);
?>

Output

This will produce following result −

rad2deg(0.78539816339745) = 45

Example

 Live Demo

Non numeric argument produces error: −

<?php
   $angle="Hello";
   echo "rad2deg(" . $angle . ") = " . rad2deg($angle);
?>

Output

This will produce following result −

PHP Warning: rad2deg() expects parameter 1 to be float, string given

Example

 Live Demo

The argument number can be a binary, octal or hexadecimal decimal number. Following example converts ox0110 angle (in radians) to degrees

<?php
   $angle=0b0110;
   echo "rad2deg(" . $angle . ") = " . rad2deg($angle);
?>

Output

This will produce following result −

rad2deg(6) = 343.77467707849
raja
Published on 30-Jun-2020 10:47:17
Advertisements