atan2()
Today, we will discuss the atan2() function in PHP. This function is used to get the arc tangent of two numbers, which is the angle whose tangent is the
The atan2() function in PHP calculates the arc tangent of two numbers, $y and $x. It returns the angle (in radians) between the positive x-axis and the point ($x, $y), which is what you need whenever you want to turn a pair of coordinates back into a direction.
This page covers the function's syntax, what it returns, the key difference between atan2() and atan(), and runnable examples.
What is the atan2() Function?
atan2() is a built-in mathematical function that returns the arc tangent of the quotient $y / $x, but — crucially — it uses the signs of both arguments to place the result in the correct quadrant. The result is expressed in radians and ranges from -π to π (about -3.14159 to 3.14159).
Syntax
atan2(float $y, float $x): floatParameters
| Parameter | Description |
|---|---|
$y | The dividend (the y-coordinate, or the value whose tangent you supply as the numerator). |
$x | The divisor (the x-coordinate, or the denominator). |
Return value
A float in the range -π to π representing the angle in radians. Convert it to degrees with rad2deg() when you need a human-readable angle.
atan2() vs atan()
The single-argument atan() function only sees $y / $x as one number, so it cannot tell the difference between, say, (1, 1) and (-1, -1) — both reduce to a ratio of 1. It always returns an angle in -π/2 to π/2 and throws away the quadrant. It can also misbehave when $x is 0 because that would be a division by zero.
atan2($y, $x) keeps both signs, so it covers the full circle (-π to π) and safely handles $x = 0. Reach for atan2() whenever your inputs are real coordinates rather than a pre-computed ratio.
How to Use the atan2() Function
Pass the y value first, then the x value, and convert the radian result to degrees if you need it:
This prints:
Radians: 1.1071487177941
Degrees: 63.434948822922We define $y and $x, pass them to atan2() to get the angle in radians, then use rad2deg() to convert that result to degrees.
Quadrant Handling
This example shows how atan2() returns a distinct angle for points in different quadrants — even when the ratio $y / $x is the same:
<?php
// Same ratio (1), but different quadrants
echo rad2deg(atan2(1, 1)), "\n"; // upper-right quadrant
echo rad2deg(atan2(-1, -1)), "\n"; // lower-left quadrant
// $x is zero — no division-by-zero error
echo rad2deg(atan2(1, 0)), "\n"; // straight up
?>Output:
45
-135
90Because atan2() inspects both signs, the points (1, 1) and (-1, -1) map to 45° and -135° respectively, and a zero $x is handled cleanly.
Conclusion
atan2() is the go-to function for converting a pair of coordinates into an angle in PHP. It returns radians in the -π to π range, correctly resolves all four quadrants, and avoids division-by-zero issues that affect the single-argument atan(). For related trigonometry, see atan(), tan(), and the deg2rad() / rad2deg() conversion helpers.