tan
Returns the tangent of an angle given in radians, equal to sin(x) divided by cos(x).
tan(float $num): float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | float | An angle expressed in radians, not degrees. |
Return value
floatThe tangent of num.
Errors
Throws a TypeError if num is not a number and cannot be converted to a float. Passing INF or NAN returns NAN.
tan() is sin($x) / cos($x) and, like every trigonometric function in PHP, it takes radians. Convert with deg2rad() if your angles come in degrees.
It is unbounded
Unlike sin() and cos(), the tangent has no upper or lower limit. It sweeps from negative infinity to positive infinity every M_PI radians, blowing up at 90 degrees, 270 degrees and so on. Because M_PI_2 is only an approximation of pi/2, PHP does not return INF at those points — it returns an enormous finite number such as 1.633e+16. Any code that divides by a tangent, or feeds it into a layout calculation, needs a guard against near-vertical angles.
The practical use is slope
The tangent of an angle is the gradient of a line at that angle: a 45 degree slope has a tangent of 1, a 30 degree slope roughly 0.577. Going the other way, atan() or atan2() converts a rise-over-run ratio back into an angle, and atan2() is the one you want because it gets the quadrant right.
Round before comparing: tan(M_PI_4) is 0.9999999999999999, not exactly 1.
Example
When to use it
- Convert a roof or ramp angle into a rise-over-run gradient
- Compute the width of a camera's field of view at a given distance
- Find the height of an object from its angle of elevation and horizontal distance
- Derive a perspective projection scale from a field-of-view angle
Frequently asked questions
Why does tan(M_PI_2) not return INF?
Because M_PI_2 is a float approximation of pi/2, not the exact value, so the argument never lands precisely on the asymptote. PHP returns roughly 1.633e+16 instead, which is finite - guard on the angle rather than testing for is_infinite().
Does tan() use degrees?
No, it uses radians like every PHP trigonometric function. Write tan(deg2rad(45)) to get the tangent of 45 degrees, which is 1.
How do I get an angle back from a slope?
Use atan($slope) for a value in radians, then rad2deg() to convert to degrees. Prefer atan2($rise, $run) when you need the correct quadrant for negative runs.
More examples
Height from an angle of elevation
tan(angle) is the ratio of the opposite side to the adjacent side, so multiplying by the horizontal distance gives the height. At 45 degrees the tangent is 1 and the height equals the distance.
<?php
$distance = 50.0; // metres to the base of a tower
foreach ([15, 30, 45, 60] as $deg) {
$height = $distance * tan(deg2rad($deg));
printf("%2d deg -> %.2f m\n", $deg, $height);
}tan() and atan() are inverses
atan() turns a gradient into an angle in radians and tan() turns it back. A slope of 1 is 45 degrees; a slope of 2 is about 63.43 degrees. Use atan2($rise, $run) instead when the sign of the run matters.
<?php
$slopes = [0.0, 0.5, 1.0, 2.0];
foreach ($slopes as $m) {
$angle = atan($m);
printf("slope %.1f = %6.2f deg, back to %.4f\n",
$m, rad2deg($angle), tan($angle));
}Guarding against the vertical asymptote
Rather than testing the tangent for infinity - which never happens - the guard checks the angle itself against 90 degrees. That keeps a near-vertical case from producing an astronomically large gradient downstream.
<?php
function slopeOf(float $deg): string {
$normalised = fmod(abs($deg), 180.0);
if (abs($normalised - 90.0) < 1e-6) {
return 'vertical';
}
return (string) round(tan(deg2rad($deg)), 4);
}
foreach ([0, 45, 89.999999, 90, 135] as $d) {
echo $d, ' -> ', slopeOf((float) $d), "\n";
}
Discussion