Signature
hypot(float $x, float $y): floatPurpose
hypot() calculates the length of the longest side of a right-angled triangle when you know the two shorter sides. Geometrically, it also gives the straight-line distance from the origin (0, 0) to the point (x, y). The manual describes it as equivalent to sqrt($x*$x + $y*$y). The dedicated function says what you mean and is the natural choice for Pythagorean-style calculations.
Signature
Parameters and defaults
$x: the length of the first side.$y: the length of the second side.
Both parameters are required and neither has a default. Because each parameter is declared as float, an integer such as 3 is accepted and converted to a float. In ordinary (non-strict) mode a numeric string such as "4" is also accepted. A non-numeric string such as "abc" causes a TypeError.
The signs of the arguments do not change the result. The calculation squares each value, so hypot(-3, 4) gives the same answer as hypot(3, 4). This is convenient when you pass coordinate differences, because the sign only tells you the direction.
Return value and failure cases
The function always returns a float, even when the answer looks like a whole number. A result of 5 is really 5.0, which echo prints as 5 but var_dump() shows as float(5). The function does not return false and does not signal failure with a warning. The only error you are likely to meet is a TypeError from an invalid argument type.
Example 1: the classic 3-4-5 triangle
The smallest useful call passes two integers and inspects the type of the result.
var_dump(hypot(3, 4));float(5)
The output shows that the result is a float, not an integer.
Example 2: distance between two points
To measure the distance between two points, subtract the coordinates to get the horizontal and vertical differences. Then pass those differences to hypot(). Negative differences are fine because the sign disappears when the values are squared.
function distance(array $a, array $b): float
{
return hypot($b[0] - $a[0], $b[1] - $a[1]);
}
echo distance([-1, -2], [2, 2]), PHP_EOL;5
Here the differences are 3 and 4, so the distance is 5.
Example 3: avoiding intermediate overflow
Writing sqrt($x*$x + $y*$y) by hand can overflow when the inputs are very large, because the squares are computed before the square root. Overflow in the squares produces INF even when the true answer is representable as a float. hypot() is meant to compute the length directly, so it handles such inputs better.
$naive = sqrt(1e200 * 1e200 + 1e200 * 1e200);
$safe = hypot(1e200, 1e200);
echo json_encode([is_infinite($naive), is_finite($safe)]), PHP_EOL;[true,true]
The hand-written formula overflows to infinity, while hypot() returns a finite number.
Edge cases
- Zero on one side:
hypot(0, 7)returns the absolute value of the other side, here7.0. - Both zero: the result is
0.0. - Floating-point rounding: most results are not exact. For example,
hypot(1, 1)is the square root of two, which is irrational. Compare computed lengths with a small tolerance rather than with==. - Special float values:
INFandNANinputs follow floating-point conventions of the underlying platform. If your data might contain them, check withis_nan(),is_infinite()oris_finite()before relying on the result. - Only two dimensions:
hypot()accepts exactly two arguments. For three or more dimensions, you can apply it repeatedly, for examplehypot(hypot($x, $y), $z). Or you can usesqrt()on the sum of squares when overflow is not a concern.
Common mistakes
- Passing the two points instead of their differences.
hypot($ax, $bx)is not a distance. Subtract first:hypot($bx - $ax, $by - $ay). - Expecting an integer. The return type is always
float, so strict comparisons with an integer such as5will fail. Use5.0or compare with a tolerance. - Using it for the missing leg. The function finds the hypotenuse. If you know the hypotenuse and one leg, compute the other leg with
sqrt($h*$h - $a*$a), and make sure the value inside the root is not negative. - Mixing degrees and lengths.
hypot()works with lengths, not angles. Useatan2()when you also need the direction of a vector. - Passing non-numeric text. Values read from forms or files are strings. Validate or cast them before calling the function so you do not trigger a
TypeError.
Related functions
sqrt()for a general square root.pow()or the**operator for squares and other powers.atan2()for the angle of a point relative to the origin.abs()for the absolute value of a single number.is_finite(),is_infinite()andis_nan()for checking special float results.
Recap
hypot($x, $y) returns the hypotenuse of a right-angled triangle, or the distance from the origin to (x, y), as a float. Signs do not matter, both arguments are required, and non-numeric input raises a TypeError. Remember to subtract coordinates first when you measure the distance between two points. The function is also safer than a hand-written square-root formula when values are very large.
Tested examples
var_dump(hypot(3, 4));float(5)
function distance(array $a, array $b): float
{
return hypot($b[0] - $a[0], $b[1] - $a[1]);
}
echo distance([-1, -2], [2, 2]), PHP_EOL;5
$naive = sqrt(1e200 * 1e200 + 1e200 * 1e200);
$safe = hypot(1e200, 1e200);
echo json_encode([is_infinite($naive), is_finite($safe)]), PHP_EOL;[true,true]