PHP sqrt() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, sqrt() returns the square root of a number.

The square root is the number that gives your original number when multiplied by itself. For example, the square root of 16 is 4.

Syntax

Call sqrt() with the values you need. Start with this line.

PHP
$root = sqrt(16);
CallWhat it does
sqrt(16)Returns 4.
sqrt(2)Returns about 1.4142135623731.
sqrt(2.25)Returns 1.5.

sqrt() always returns a float, even for perfect squares. For other roots, use pow() with a fractional exponent, such as pow(27, 1 / 3).

Library declarations
PHP
sqrt(float $num): float

Parameters

ParameterDescription
numRequired. The number to find the square root of. It should be 0 or greater.

Return Value

Return ValueDescription
floatThe square root of num. Returns NAN for negative numbers.

PHP Examples

Example 1 — Find Square Roots

Pass a number to sqrt() and get its square root.

PHP
<?php
echo sqrt(16), PHP_EOL;
echo sqrt(2), PHP_EOL;
echo sqrt(2.25), PHP_EOL;

How It Works

  1. sqrt(16) returns 4 because 4 x 4 is 16.
  2. sqrt(2) is not a whole number, so PHP prints a long decimal.
  3. sqrt(2.25) returns 1.5 because 1.5 x 1.5 is 2.25.
  4. PHP prints floats with up to 14 significant digits.

Example 2 — Zero and Negative Numbers

Square roots are only real for numbers that are 0 or greater.

PHP
<?php
echo sqrt(0), PHP_EOL;
echo sqrt(-4), PHP_EOL;
echo is_nan(sqrt(-4)) ? "Cannot take the root" : "OK", PHP_EOL;

How It Works

  1. sqrt(0) is 0.
  2. sqrt(-4) has no real answer, so PHP returns NAN.
  3. is_nan() checks whether the result is NAN.
  4. The message tells the user the root could not be calculated.

Example 3 — Check for a Perfect Square

Use sqrt() and floor() together to test whether a number is a perfect square.

PHP
<?php
$numbers = [49, 50];

foreach ($numbers as $n) {
    $root = sqrt($n);
    if ($root == floor($root)) {
        echo $n, " is a perfect square", PHP_EOL;
    } else {
        echo $n, " is not a perfect square", PHP_EOL;
    }
}

How It Works

  1. Loop over two numbers: 49 and 50.
  2. sqrt() gives 7 for 49 and about 7.07 for 50.
  3. floor() removes the decimals, so we can compare the root with its whole part.
  4. If both are equal, the number is a perfect square.

Frequently Asked Questions

sqrt() returns the square root of a number as a float.
You passed a negative number. Real square roots exist only for 0 and positive numbers.
Use pow($n, 1 / 3) for positive numbers. PHP does not have a built-in cbrt() function.
Yes, they give the same result for non-negative numbers. sqrt() is faster to read and clearer.
No. It always returns a float. Cast with (int) if you need an integer, for example (int) sqrt(16).

Next: tan()

tan() returns the tangent of an angle given in radians.

tan() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful