float sqrt ( float $number )
$number is the input value for which square root is required.
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.
$a=3; // One side
$b=4; // One side
$h=sqrt(pow($a,2)+pow($b,2));
echo $h;
Output is 5$num = 0.25;
echo sqrt($num); // Output: 0.5
$large_num = 1000000;
echo sqrt($large_num); // Output: 1000
$num = -16;
echo sqrt($num); // Output: NAN (PHP does not return real square roots for negative numbers)
$num = 0;
echo sqrt($num); // Output: 0
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.
Author
🎥 Join me live on YouTubePassionate 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.