atan()
Today, we will discuss the atan() function in PHP. This function is used to get the arc tangent of a number, which is the angle whose tangent is the given
The atan() function in PHP returns the arc tangent of a number — the angle whose tangent equals that number. The result is always expressed in radians. This page covers the syntax, parameters, return value, the range of results you can expect, and how atan() relates to its sibling functions like tan() and atan2().
What is the atan() Function?
Arc tangent (often written as arctan or tan⁻¹) is the inverse of the tangent function. If tan(θ) = x, then atan(x) = θ. So atan() answers the question: "Which angle has this tangent?"
The function accepts a single numeric argument and returns the angle in radians. Because tangent is periodic and repeats infinitely, atan() always returns the angle in the principal range −π/2 to π/2 (roughly −1.5708 to 1.5708 radians, or −90° to 90°).
Syntax
atan(float $num): float| Part | Description |
|---|---|
$num | The value to compute the arc tangent of. Any real number is valid. |
| Return | The arc tangent of $num, in radians, in the range −M_PI/2 to M_PI/2. |
How to Use the atan() Function
Pass a number and read back the angle whose tangent is that number:
This prints 0.46364760900081. The value is in radians, not degrees.
Converting the Result to Degrees
Since atan() returns radians, you usually convert to degrees for human-readable output using rad2deg():
<?php
$radians = atan(1); // tangent of the angle is 1
echo $radians, "\n"; // 0.78539816339745 (π/4)
echo rad2deg($radians); // 45
?>atan(1) is π/4 because tan(45°) = tan(π/4) = 1.
Useful Reference Values
These results show the principal range and the symmetry of the function:
<?php
echo atan(0), "\n"; // 0
echo atan(1), "\n"; // 0.78539816339745 (45°)
echo atan(-1), "\n"; // -0.78539816339745 (-45°)
echo atan(100), "\n"; // 1.5607966601082 (approaches π/2)
echo atan(-100); // -1.5607966601082 (approaches -π/2)
?>As the input grows toward positive infinity the result approaches π/2 but never reaches it; toward negative infinity it approaches −π/2. The function is odd: atan(-x) === -atan(x).
atan() vs atan2()
atan() takes one argument and cannot tell which quadrant the original angle was in, so its output is limited to −π/2…π/2. When you have both the y and x components of a point or vector, use atan2() instead — it considers the signs of both arguments and returns the full angle in the range −π to π:
<?php
echo atan(1 / 1), "\n"; // 0.78539816339745 (45°)
echo atan2(1, 1), "\n"; // 0.78539816339745 (45°, same here)
echo atan2(-1, -1); // -2.3561944901923 (-135°, correct quadrant)
?>atan(-1 / -1) would collapse to atan(1) and lose the quadrant, which is why atan2() is preferred for direction and angle-of-a-vector calculations.
Common Use Cases
- Finding the angle of a slope or line from its gradient.
- Computing the direction of a 2D vector (prefer
atan2()when both components are known). - Inverse-trig math in physics, graphics, and geometry code.
Related Functions
tan()— the tangent of an angle (the inverse operation).atan2()— arc tangent of two variables, with correct quadrant.atanh()— the inverse hyperbolic tangent.asin()andacos()— arc sine and arc cosine.rad2deg()anddeg2rad()— convert between radians and degrees.
Conclusion
The atan() function returns the arc tangent of a number as an angle in radians, always within −π/2 to π/2. Remember that the result is in radians (convert with rad2deg() when you need degrees), and reach for atan2() whenever you need the correct quadrant from separate y and x values.