PHP Math functions sqrt

float sqrt ( float $number )
$number is the input value for which square root is required.

We can get square root of a math number by using sqrt function
Here are some examples
echo sqrt(16);// output is 4
echo sqrt(20);// output 4.4721359549996
echo sqrt(-9);// output NAN
echo sqrt(-2.5);// output NAN
echo sqrt(1);// output 1
echo sqrt(-1);// output 
Note that for negative numbers we will get output as NAN mean not a number . We can verify this output by using is_nan function.

Hypotenuse of a right angle triangle

In a right angled triangle we have two sides of right angle as a & b. The hypotenuse of the triangle is known as h . We have to find the value of it.

We will use POW function to get the square of each side. Then we will add them and take the square root of the sum.

Here is the code.
$a=3;  // One side
$b=4;  // One side
$h=sqrt(pow($a,2)+pow($b,2));
echo $h;
Output is 5

Example: Square Root of a Fraction

$num = 0.25;
echo sqrt($num);  // Output: 0.5

Example: Square Root of a Large Number

$large_num = 1000000;
echo sqrt($large_num);  // Output: 1000

Example: Handling Negative Numbers with sqrt()

$num = -16;
echo sqrt($num);  // Output: NAN (PHP does not return real square roots for negative numbers)

Example: Square Root of Zero

$num = 0;
echo sqrt($num);  // Output: 0

Example: Square Root in Loops

for ($i = 1; $i <= 5; $i++) {
    echo "Square root of $i is " . sqrt($i) . "
"; } // Output: // Square root of 1 is 1 // Square root of 2 is 1.4142135623731 // Square root of 3 is 1.7320508075689 // Square root of 4 is 2 // Square root of 5 is 2.2360679774998
These examples demonstrate how to calculate the square root of fractions, large numbers, and the behavior when negative numbers are passed.
Math Functions SIN() function rad2deg() function
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer