PHP atan2() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP atan2() function returns the angle, in radians, between the positive x-axis and the point (x, y).

Unlike atan(), it uses the signs of both numbers, so it always gives the correct angle for the right quarter of the circle.

Syntax

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

PHP
$angle = atan2($y, $x);
CallWhat it does
atan2(1, 1)Returns about 0.7854 (45 degrees) for a point in the top-right area.
atan2(1, -1)Returns about 2.3562 (135 degrees) for a point in the top-left area.
atan2(-1, -1)Returns about -2.3562 (-135 degrees) for a point in the bottom-left area.

Remember the order of the arguments: y comes first, then x.

Library declarations
PHP
atan2(float $y, float $x): float

Parameters

ParameterDescription
yRequired. The y coordinate (vertical distance) of the point.
xRequired. The x coordinate (horizontal distance) of the point.

Return Value

Return ValueDescription
floatThe angle in radians, between -pi and pi.

PHP Examples

Example 1 — Basic atan2() usage

This example finds the angle for points in four different areas of the plane.

PHP
<?php
echo round(atan2(1, 1), 4) . PHP_EOL;
echo round(atan2(1, -1), 4) . PHP_EOL;
echo round(atan2(-1, -1), 4) . PHP_EOL;
echo round(atan2(-1, 1), 4) . PHP_EOL;

How It Works

  1. atan2(1, 1) is a point in the top-right area, giving 45 degrees (0.7854 radians).
  2. atan2(1, -1) is in the top-left area, giving 135 degrees (2.3562 radians).
  3. atan2(-1, -1) is in the bottom-left area, giving -135 degrees (-2.3562 radians).
  4. atan2(-1, 1) is in the bottom-right area, giving -45 degrees (-0.7854 radians).

Example 2 — atan2() versus atan()

atan(y / x) gives the same answer for (1, 1) and (-1, -1), but they are in opposite directions. atan2() keeps them apart.

PHP
<?php
echo "atan(1 / -1):  " . round(atan(1 / -1), 4) . PHP_EOL;
echo "atan2(1, -1):  " . round(atan2(1, -1), 4) . PHP_EOL;
echo "atan2(0, -1):  " . round(atan2(0, -1), 4) . PHP_EOL;
echo "atan2(1, 0):   " . round(atan2(1, 0), 4) . PHP_EOL;

How It Works

  1. 1 / -1 equals -1, so atan() only sees the slope -1 and returns -0.7854.
  2. atan2(1, -1) knows x is negative and y is positive, so it returns 2.3562.
  3. atan2(0, -1) points straight left, which is pi (3.1416).
  4. atan2(1, 0) points straight up and does not divide by zero, so it returns pi / 2.

Example 3 — Angle of a point in degrees

Let us find the angle of a point 4 units to the right and 3 units up, in degrees.

PHP
<?php
$x = 4;
$y = 3;

$radians = atan2($y, $x);
$degrees = rad2deg($radians);

echo "Point ($x, $y)" . PHP_EOL;
echo "Radians: " . round($radians, 4) . PHP_EOL;
echo "Degrees: " . round($degrees, 2) . PHP_EOL;

How It Works

  1. We set x to 4 and y to 3.
  2. atan2($y, $x) returns the angle of the point in radians.
  3. rad2deg() converts the angle to degrees.
  4. We round the values to make the output short and easy to read.

Frequently Asked Questions

atan2() returns the angle between the positive x-axis and the point (x, y), in radians, from -pi to pi.
It follows the common math convention atan2(y, x), which comes from the idea of y divided by x. Just remember y first, x second.
atan2() uses the signs of both numbers, so it gets the correct quarter of the circle. It also works when x is 0 without a division by zero.
Wrap the result in rad2deg(), for example rad2deg(atan2(3, 4)).
PHP returns 0 for atan2(0, 0).

Next: atanh()

atanh() function returns the inverse hyperbolic tangent of a number.

atanh() 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