How to Returns the tangent of a number in PHP ?


In PHP, we can use the tan() function to return the tangent of a number. The tan() function takes one argument, which is the angle in radians.

Syntax:

tan($angle)

Example:

$angle = pi()/4; // 45 degrees in radians
$tan = tan($angle);
echo "The tangent of $angle radians is: $tan"; // Output: The tangent of 0.78539816339745 radians is: 0.99999999999977

Alternatively, we can also use the tan() function from the Math class in the bcmath extension to calculate the tangent of a number with arbitrary precision.

Syntax:

bcdiv(bcsqrt(bcpow($x, 2) + 1), $x, $precision)

Example:

$x = 2.5;
$precision = 10;
$tan = bcdiv(bcsqrt(bcpow($x, 2) + 1), $x, $precision);
echo "The tangent of $x is: $tan"; // Output: The tangent of 2.5 is: 0.7470222973


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.