hypot() function in PHP

The hypot() function is used to calculate the hypotenuse of a right-angle triangle. It returns the length of the hypotenuse as a float. In a right-angled triangle, the hypotenuse is the longest side opposite to the right angle.

a b hypotenuse

Syntax

hypot(a, b)

Parameters

  • a − Length of first side (numeric value)

  • b − Length of second side (numeric value)

Return Value

The hypot() function returns the length of the hypotenuse as a float value.

Example

Here's how to calculate hypotenuse for different triangles −

<?php
    echo "Triangle 1: " . hypot(3, 4) . "<br>";
    echo "Triangle 2: " . hypot(5, 12) . "<br>";
    echo "Triangle 3: " . hypot(8, 15) . "<br>";
    echo "Triangle 4: " . hypot(7, 24) . "<br>";
?>

Output

Triangle 1: 5
Triangle 2: 13
Triangle 3: 17
Triangle 4: 25

Practical Application

Calculate the diagonal length of a rectangle −

<?php
    $length = 6;
    $width = 8;
    $diagonal = hypot($length, $width);
    echo "Rectangle diagonal: " . $diagonal;
?>

Output

Rectangle diagonal: 10

Conclusion

The hypot() function provides an efficient way to calculate hypotenuse length using the Pythagorean theorem. It's useful for geometry calculations, distance measurements, and mathematical applications.

Updated on: 2026-03-15T07:27:52+05:30

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements