PHP tan() function


The tan() function in PHP is used to calculate the tangent of a given angle, which is expected to be in radians. The tangent function is a fundamental trigonometric function defined as the ratio of the sine of an angle to the cosine of that angle. It can also be understood in terms of a right-angled triangle, where the tangent of an angle is the ratio of the length of the opposite side to the length of the adjacent side.

Syntax:

tan($angle_in_radians)

Parameters:

  • $angle_in_radians: The angle in radians for which you want to calculate the tangent value.

Return Value:

  • The function returns the tangent of the given angle as a float.

Important Note:

Since the tan() function expects the angle in radians, if your angle is in degrees, you need to convert it to radians using the deg2rad() function.

Example 1: Calculating Tangent of an Angle in Radians

<?php $angle_in_radians = pi() / 4; // pi/4 radians (45 degrees) // Calculate tangent of the angle $tangent_value = tan($angle_in_radians); echo $tangent_value; ?>

Output:

1

In this example, tan(π/4)\tan(\pi/4) or tan(45)\tan(45^\circ) is 1.

Example 2: Calculating Tangent of an Angle in Degrees

To calculate the tangent of an angle given in degrees, convert it to radians using deg2rad().

<?php $angle_in_degrees = 60; // Convert degrees to radians $angle_in_radians = deg2rad($angle_in_degrees); // Calculate tangent of the angle $tangent_value = tan($angle_in_radians); echo $tangent_value; ?>

Output:

1.7320508075689

Here, tan(60)\tan(60^\circ) is approximately 1.7321.

Practical Usage:

  • The tan() function is widely used in trigonometric calculations, particularly in geometry, physics, and engineering.
  • It is useful in applications involving slopes, angles, and periodic functions, such as wave motion or circular motion.

Example 3: Using tan() in Physics (Inclined Plane)

In physics, the tangent of the angle of an inclined plane is used to find the relationship between the height hh and the length ll of the plane:

tan(θ)=hl\tan(\theta) = \frac{h}{l}

Where:

  • hh is the height of the incline,
  • ll is the length of the incline,
  • θ\theta is the angle of inclination.

To find the height given the length and angle, you can rearrange the equation:

h=ltan(θ)h = l \cdot \tan(\theta)

<?php $l = 10; // Length of the incline in meters $angle_in_degrees = 30; // Angle of inclination in degrees // Convert degrees to radians $angle_in_radians = deg2rad($angle_in_degrees); // Calculate height using tangent $height = $l * tan($angle_in_radians); echo "The height of the incline is: " . $height . " meters"; ?>

Output:

The height of the incline is: 5.7735026918963 meters

Summary:

  • tan() calculates the tangent of an angle in radians.
  • If the angle is in degrees, use deg2rad() to convert it to radians before using tan().
  • It is widely used in mathematical, physical, and engineering applications to compute angles, slopes, and periodic functions.