PHP tan() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, tan() returns the tangent of an angle given in radians.

Tangent is a basic trigonometry function equal to sine divided by cosine. If your angle is in degrees, convert it with deg2rad() first.

Syntax

Call tan() with the values you need. Start with this line.

PHP
$result = tan(M_PI_4);
CallWhat it does
tan(0)Returns 0.
tan(M_PI_4)Returns about 1, the tangent of 45 degrees.
tan(deg2rad(60))Returns about 1.7321.

The angle must be in radians. Tangent is undefined at 90 degrees (pi / 2), where PHP returns a huge number instead of an error. Use round() to hide tiny floating-point errors.

Library declarations
PHP
tan(float $num): float

Parameters

ParameterDescription
numRequired. The angle in radians.

Return Value

Return ValueDescription
floatThe tangent of the angle. Returns NAN for infinite input.

PHP Examples

Example 1 — Tangent of Common Angles

Try tan() with a few familiar angles.

PHP
<?php
echo tan(0), PHP_EOL;
echo round(tan(M_PI_4), 2), PHP_EOL;
echo round(tan(deg2rad(60)), 4), PHP_EOL;

How It Works

  1. tan(0) is 0 because sine of 0 is 0.
  2. M_PI_4 is 45 degrees, and its tangent is 1.
  3. deg2rad(60) converts 60 degrees to radians.
  4. The tangent of 60 degrees is about 1.7321 after rounding.

Example 2 — Tangent Near 90 Degrees

Tangent is undefined at 90 degrees, so the result becomes enormous.

PHP
<?php
echo tan(M_PI_2), PHP_EOL;
echo tan(INF), PHP_EOL;

How It Works

  1. M_PI_2 is a close approximation of 90 degrees, not an exact value.
  2. That is why PHP returns a huge number instead of a real infinity.
  3. E+16 means the number is multiplied by 10 to the power 16.
  4. tan(INF) is undefined, so PHP returns NAN.

Example 3 — Find the Height of a Tree

Tangent helps with right-angled triangles. Here we find a height from a distance and an angle.

PHP
<?php
$distance = 10;
$angle = 30;
$height = $distance * tan(deg2rad($angle));

echo "Height: ", round($height, 2), " m", PHP_EOL;

How It Works

  1. You stand 10 metres from a tree and look up at a 30 degree angle.
  2. deg2rad() converts the angle to radians.
  3. Height equals distance times the tangent of the angle.
  4. Round to two decimals and print the height.

Frequently Asked Questions

tan() returns the tangent of an angle in radians. It equals sin($x) / cos($x).
Radians. Use tan(deg2rad($degrees)) when you start with degrees.
Tangent is undefined at exactly 90 degrees. Because M_PI_2 is only a close approximation, PHP returns a very large number, not INF.
The inverse is atan(), which returns the angle in radians for a given tangent value. atan2() is even better when you have x and y values.
Any float. Unlike sin() and cos(), tangent is not limited to -1 to 1.

Next: tanh()

tanh() returns the hyperbolic tangent of a number.

tanh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful