atan2() function in PHP

The atan2() function in PHP returns the arc tangent of two variables. It calculates the angle in radians between the positive x-axis and the ray from the origin to the point (val2, val1).

Syntax

atan2(y, x)

Parameters

  • y − The y-coordinate (dividend)

  • x − The x-coordinate (divisor)

Return Value

The atan2() function returns the arc tangent of y/x in radians. The returned value is between -? and ?, which represents the angle from the positive x-axis to the point (x, y).

Example 1

Basic usage with positive values −

<?php
    echo atan2(30, 30) . "<br>";
    echo atan2(2, 2) . "<br>";
    echo atan2(1, 1) . "<br>";
?>
0.78539816339745
0.78539816339745
0.78539816339745

Example 2

Using negative values to demonstrate different quadrants −

<?php
    echo atan2(-15, -15) . "<br>";
    echo atan2(-0.90, -0.70) . "<br>";
    echo atan2(1, -1) . "<br>";
    echo atan2(-1, 1) . "<br>";
?>
-2.3561944901923
-2.2318394956456
2.3561944901923
-0.78539816339745

Converting to Degrees

To convert the result from radians to degrees, multiply by 180/? or use rad2deg()

<?php
    $radians = atan2(1, 1);
    $degrees = rad2deg($radians);
    
    echo "Radians: " . $radians . "<br>";
    echo "Degrees: " . $degrees . "<br>";
?>
Radians: 0.78539816339745
Degrees: 45

Conclusion

The atan2() function is useful for calculating angles in coordinate systems and is more robust than atan() as it handles all four quadrants correctly. Use rad2deg() to convert results to degrees when needed.

Updated on: 2026-03-15T07:25:40+05:30

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements