PHP tan() Function

The tan() function returns the tangent ratio of a given angle in radians. In trigonometry, tangent of an angle is defined as the ratio of lengths of opposite side and adjacent side.

tan(x) = opposite/adjacent

Tangent of an angle is also defined as the ratio of its sine and cosine:

tan(x) = sin(x)/cos(x)

If x = 45 degrees, tan(x) = 1, as in a right-angled triangle, opposite and adjacent sides are equal. This function returns a float value.

Syntax

tan ( float $arg ) : float

Parameters

Sr.No Parameter & Description
1 arg
A floating point value that represents angle in radians

Return Value

PHP tan() function returns the tangent ratio of the given parameter as a float.

Example 1: Basic Usage

Here's how to calculate tangent of 0 radians ?

<?php
    $arg = 0;
    $val = tan($arg);
    echo "tan(" . $arg . ") = " . $val;
?>
tan(0) = 0

Example 2: Common Angles

Let's calculate tangent for different angle values ?

<?php
    // Common angles in radians
    $angles = [0, pi()/6, pi()/4, pi()/3, pi()/2];
    $degrees = [0, 30, 45, 60, 90];
    
    for($i = 0; $i < count($angles); $i++) {
        $result = tan($angles[$i]);
        echo "tan({$degrees[$i]}°) = " . round($result, 4) . "<br>";
    }
?>
tan(0°) = 0
tan(30°) = 0.5774
tan(45°) = 1
tan(60°) = 1.732
tan(90°) = 1.633123935319E+16

Key Points

  • The function accepts angles in radians, not degrees
  • For degree-to-radian conversion: radians = degrees × ?/180
  • tan(90°) approaches infinity, returning a very large number
  • Available in PHP 4.x, 5.x, 7.x, and 8.x

Conclusion

The tan() function is essential for trigonometric calculations in PHP. Remember to convert degrees to radians when working with degree measurements, and handle special cases like 90° carefully.

Updated on: 2026-03-15T08:59:42+05:30

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements