tan() function in PHP

The tan() function in PHP returns the tangent of a specified value in radians. It's a built-in mathematical function that calculates the ratio of sine to cosine for a given angle.

Syntax

tan(value)

Parameters

  • value − A numeric value representing an angle in radians (float or integer)

Return Value

The tan() function returns a float representing the tangent of the specified angle. Returns NaN for invalid inputs.

Basic Examples

Here are some basic examples demonstrating the tan() function ?

<?php
    echo tan(0) . "<br>";
    echo tan(1) . "<br>";
    echo tan(M_PI_4); // ?/4 radians (45 degrees)
?>
0<br>1.5574077246549<br>1

Using Decimal Values

The function works with decimal radian values as well ?

<?php
    echo tan(0.50) . "<br>";
    echo tan(-0.50);
?>
0.54630248984379<br>-0.54630248984379

Negative Values

The tangent function handles negative angles correctly ?

<?php
    echo tan(-5) . "<br>";
    echo tan(-10);
?>
3.3805150062466<br>-0.64836082745909

Key Points

  • Input must be in radians, not degrees
  • Use deg2rad() to convert degrees to radians if needed
  • The function returns undefined (very large numbers) at ?/2, 3?/2, etc.
  • Common values: tan(0) = 0, tan(M_PI_4) = 1

Conclusion

The tan() function is essential for trigonometric calculations in PHP. Remember to work with radians and use appropriate constants like M_PI_4 for common angles.

Updated on: 2026-03-15T07:31:58+05:30

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements