sqrt() function in PHP

The sqrt() function in PHP returns the square root of a number. It accepts any numeric value and returns a float representing the square root.

Syntax

sqrt(num)

Parameters

  • num − The number for which you want to find the square root. Can be an integer or float.

Return Value

The sqrt() function returns the square root of the specified number as a float. For negative numbers, it returns NAN (Not a Number).

Examples

Basic Square Root

Finding the square root of a perfect square ?

<?php
    echo sqrt(16);
?>
4

Decimal Numbers

The function also works with decimal numbers ?

<?php
    echo sqrt(0.25) . "<br>";
    echo sqrt(2.25);
?>
0.5
1.5

Negative Numbers

When dealing with negative numbers, sqrt() returns NAN ?

<?php
    echo sqrt(-144);
?>
NAN

Multiple Values

Computing square roots for multiple values ?

<?php
    $numbers = [4, 9, 25, 100];
    foreach ($numbers as $num) {
        echo "sqrt($num) = " . sqrt($num) . "<br>";
    }
?>
sqrt(4) = 2
sqrt(9) = 3
sqrt(25) = 5
sqrt(100) = 10

Conclusion

The sqrt() function is useful for mathematical calculations requiring square root operations. Remember that it returns NAN for negative inputs and always returns a float value.

Updated on: 2026-03-15T07:31:46+05:30

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements