Trigonometry plays a crucial role in mathematics, physics, engineering, and various other fields. In PHP, you have a powerful set of trigonometric functions at your disposal to perform complex calculations with ease. In this article, we will explore these PHP trigonometric functions, providing examples of how to use each one effectively. You can also reference the articles here and here that outline additional information on trig functions.
The Fundamentals: Pi in PHP
In trigonometry, one fundamental constant is π (pi). PHP makes it simple to access the value of pi using the pi()
function, which returns the constant value 3.1415926535898.
echo pi(); // 3.1415926535898
Converting Degrees to Radians with deg2rad()
When working with angles, you often need to convert between degrees and radians. PHP provides the deg2rad()
function for this purpose. It multiplies the degrees by π and divides the result by 180.
echo deg2rad(0); echo deg2rad(30); echo deg2rad(60); echo deg2rad(90); // Output: // 0.0 // 0.5235987755982988 // 1.0471975511965976 // 1.5707963267948966
Converting Radians to Degrees with rad2deg()
Conversely, you can convert radians back to degrees using the rad2deg()
function. It multiplies the radians by 180 and divides by π.
echo rad2deg(0); echo rad2deg(pi()/6); echo rad2deg(pi()/3); echo rad2deg(pi()/2); // Output: // 0 // 30 // 60 // 90
Calculating Sine with sin()
To calculate the sine of a number in radians, you can use the sin()
function. The input must be a float, and the return value will be a float ranging from -1 to 1.
echo sin(pi()/3); echo sin(0); echo sin(pi()/2); // Output: // 0.8660254037844386 // 0.0 // 1.0
Calculating Cosine with cos()
For finding the cosine of a number in radians, PHP provides the cos()
function. It requires a float as input, and it returns a float between -1 and 1.
echo cos(pi()/3); echo cos(0); echo cos(pi()/2); // Output: // 0.5000000000000001 // 1.0 // 6.123233995736766e-17
Determining Tangent with tan()
To calculate the tangent of a number, or the sine divided by the cosine of an angle in radians, use the tan()
function. The input must be a float, and the return value is a float ranging from negative infinity to infinity.
echo tan(pi()/3); echo tan(0); echo tan(pi()/2); // Output: // 1.7320508075688767 // 0.0 // 1.633123935319537e+16
Finding Arcsine with asin()
The asin()
function is used to find the arcsine of a number. It requires a float between -1 and 1 as input and returns a float between -π/2 and π/2 radians.
echo asin(0.5); echo asin(0); echo asin(-0.75); // Output: // 0.5235987755982989 // 0.0 // -0.848062078981481
Finding Arccosine with acos()
To determine the arccosine of a number, use the acos()
function. It requires a float between -1 and 1 as input and returns a float between 0 and π radians.
echo acos(0.5); echo acos(0); echo acos(-0.75); // Output: // 1.0471975511965979 // 1.5707963267948966 // 2.4188584057763776
Calculating Arctangent with atan()
The atan()
function allows you to find the arctangent of a number. It requires a float as input and returns a float between -π/2 and π/2 radians.
echo atan(5); echo atan(0); echo atan(-3); // Output: // 1.373400766945016 // 0.0 // -1.2490457723982544
Handling the Quotient of Two Numbers with atan2()
For finding the arctangent of the quotient of two numbers, represented as the coordinates of a point (x, y), you can use the atan2()
function. The inputs must be floats, and the return value is a float between -π and π radians.
echo atan2(5, 1); echo atan2(0, 0); echo atan2(-3, 7); // Output: // 1.373400766945016 // 0.0 // -0.40489178628508343
Hyperbolic Functions
PHP also provides hyperbolic trigonometric functions:
sinh()
for hyperbolic sinecosh()
for hyperbolic cosinetanh()
for hyperbolic tangentasinh()
for hyperbolic arcsineacosh()
for hyperbolic arccosineatanh()
for hyperbolic arctangent
These functions operate in a similar manner to their standard trigonometric counterparts.
Conclusion
Trigonometry is an essential branch of mathematics, and PHP’s extensive collection of trigonometric functions simplifies complex calculations in various applications. Whether you’re working with angles, coordinates, or hyperbolic values, PHP’s functions provide the tools you need to perform accurate and efficient calculations in your web applications and scripts. We hope this article has been helpful in demystifying PHP’s trigonometric functions and their applications.