PHP hypot() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The hypot() function calculates length of hypotenus of a right angled triangle. Hypoteneuse is calculated as per following formula −

h=sqrt(x2+y2) where x and y are other two sides of a right angled triangle

For example, if x=3 and y=4, hypot(x,y)=5 which is equal to sqrt(32+42) = sqrt(25) =5

This function always returns a float.

Syntax

hypot ( float $x , float $y ) : float

Parameters

Sr.NoParameter & Description
1x
one side of right angled triangle
2y
other side of right angled triangle

Return Values

PHP hypot() function returns length of hypotenuse of a right angled triangle with given values of x and y

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example computes hypotenuse of a right angle triangle with sides 3 and 4−

<?php
   $x = 3;
   $y = 4;
   echo "hypot(" . $x . "," . $y . ") = " . hypot($x, $y);
?>

Output

This returns largest integer−

hypot(3,4) = 5

Example

 Live Demo

Return value of hypot(x,y) function is equal to sqrt(x*x+y*y) as per pythogoras theorem. Following example confirms it−

<?php
   $x = 3;
   $y = 4;
   echo "hypot(" . $x . "," . $y . ") = " . hypot($x, $y) ."\n";
   echo "Hypotenuse calculated by Pythogoras equation = " . sqrt(pow($x,2)+pow($y,2));
?>

Output

This returns following output−

hypot(3,4) = 5
Hypotenuse calculated by Pythogoras equation = 5
raja
Published on 29-Jun-2020 12:29:14
Advertisements