PHP hypot() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The hypot() function calculates the length of the hypotenuse of a right-angled triangle when you know the two shorter sides.

It returns sqrt(x*x + y*y) and is also the easiest way to find the distance between two points.

Syntax

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

PHP
You pass the two side lengths to hypot() and it returns the length of the longest side as a float.
CallWhat it does
hypot(3, 4)Returns 5, the classic 3-4-5 triangle.
hypot(5, 12)Returns 13.
hypot(1, 1)Returns about 1.4142, which is the square root of 2.

Negative side lengths are fine because the numbers are squared, so hypot(-3, -4) is also 5.

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

Parameters

ParameterDescription
xRequired. The length of the first side of the triangle.
yRequired. The length of the second side of the triangle.

Return Value

Return ValueDescription
floatThe length of the hypotenuse, calculated as sqrt(x*x + y*y).

PHP Examples

Example 1 — Find the longest side

Calculate the hypotenuse for a few right triangles.

PHP
<?php
echo hypot(3, 4) . PHP_EOL;
echo hypot(5, 12) . PHP_EOL;
echo round(hypot(1, 1), 4) . PHP_EOL;

How It Works

  1. For sides 3 and 4, 9 + 16 = 25 and the square root of 25 is 5.
  2. For sides 5 and 12, 25 + 144 = 169 and the square root is 13.
  3. For sides 1 and 1, the result is the square root of 2, about 1.4142.
  4. round() keeps the last result short.

Example 2 — Zero, negatives and huge numbers

See how hypot() handles special inputs.

PHP
<?php
echo hypot(0, 0) . PHP_EOL;
echo hypot(-3, -4) . PHP_EOL;
echo hypot(1e200, 1e200) . PHP_EOL;
echo sqrt(1e200 * 1e200 + 1e200 * 1e200) . PHP_EOL;

How It Works

  1. Two zero sides give a hypotenuse of 0.
  2. Negative values are squared, so -3 and -4 give the same answer as 3 and 4.
  3. hypot(1e200, 1e200) works correctly even though the squares would be too large.
  4. The manual sqrt() version overflows to INF, which shows why hypot() is safer.

Example 3 — Distance between two points

Use the difference in x and y as the two sides of a triangle.

PHP
<?php
$x1 = 1;
$y1 = 2;
$x2 = 4;
$y2 = 6;

$distance = hypot($x2 - $x1, $y2 - $y1);

echo "Distance: " . $distance . PHP_EOL;

How It Works

  1. The horizontal difference is 4 - 1 = 3.
  2. The vertical difference is 6 - 2 = 4.
  3. These two numbers are the short sides of a right triangle.
  4. hypot(3, 4) returns 5, which is the straight line distance between the points.

Frequently Asked Questions

It returns the hypotenuse of a right triangle, which is sqrt(x*x + y*y).
hypot() is shorter and it avoids overflow or underflow when the numbers are very big or very small.
Yes. Pass the difference in x and the difference in y, like hypot($x2 - $x1, $y2 - $y1).
Yes. The values are squared, so the sign does not change the result.
No, it always returns a float. PHP just prints 5.0 as 5.

Next: intdiv()

The intdiv() function divides two whole numbers and returns only the whole number part of .

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