Mathematics7 min readExamples run on PHP 8.5.10

sqrt()

Learn how PHP 8.4 sqrt() returns the square root of a number, how it handles negative input with NAN, and how to avoid common mistakes.

Signature

sqrt(float $num): float

Purpose

sqrt() calculates the square root of a number: the value that, multiplied by itself, gives the original number. It is a basic building block for geometry (distances, diagonals), statistics (standard deviation), and any formula that involves a root. It is available in every supported PHP version and needs no extension.

Signature

Parameters and defaults

$num is the only parameter and it has no default, so you must always pass it. It is declared as float. In PHP's default (non-strict) mode, an integer such as 9 or a numeric string such as "16" is converted automatically. With declare(strict_types=1), integers are still accepted for float parameters, but strings are rejected. A non-numeric string such as "abc" causes a TypeError.

Return value and failure cases

The function always returns a float, even for perfect squares. sqrt(9) is the float 3.0, not the integer 3. When printed with echo it shows as 3, which can hide the type.

For a negative argument, sqrt() does not throw an exception or raise a warning. It returns the special value NAN (not a number). Because NAN is not equal to anything, including itself, you must test it with is_nan() rather than == or ===.

Example 1: basic roots

This shows an integer perfect square, zero and a decimal input. The printed form of a float depends on the precision setting, so very long irrational results are best formatted explicitly.

Example
<?php
echo sqrt(9), PHP_EOL;
echo sqrt(0), PHP_EOL;
echo sqrt(2.25), PHP_EOL;
Output
3
0
1.5

Example 2: guarding against negative input

A real square root does not exist for negative numbers, so a wrapper that checks the input first makes the failure explicit. The wrapper below returns null for invalid input, and the caller decides what to display.

Example
<?php
function safeSqrt(float $n): ?float
{
    return $n < 0 ? null : sqrt($n);
}

foreach ([16, -1] as $n) {
    $r = safeSqrt($n);
    echo $r === null ? 'undefined' : $r, PHP_EOL;
}
Output
4
undefined

Alternatively, call sqrt() directly and check the result with is_nan(). That works, but validating before the call usually makes the intent clearer.

Example 3: distance between two points

The Pythagorean theorem gives the straight-line distance between two points. Squaring the differences and taking the root is the classic use of sqrt(). Here number_format() fixes the number of decimals so the output does not depend on the precision setting.

Example
<?php
function distance(float $x1, float $y1, float $x2, float $y2): float
{
    return sqrt(($x2 - $x1) ** 2 + ($y2 - $y1) ** 2);
}

echo distance(1, 2, 4, 6), PHP_EOL;
echo number_format(distance(0, 0, 1, 1), 4), PHP_EOL;
Output
5
1.4142

Edge cases

  • Zero: sqrt(0) is 0.0.
  • Negative numbers: the result is NAN, with no exception.
  • Infinity: the square root of positive infinity is positive infinity, and NAN input stays NAN.
  • Floating-point error: most roots are irrational and are stored approximately. Two mathematically equal results may differ in the last digits, so compare with a small tolerance instead of ===.
  • Very large integers: an integer beyond the float's exact range is converted to float first, so precision can be lost before the root is taken.

Common mistakes

  1. Comparing with NAN directly. $root === NAN is always false. Use is_nan($root).
  2. Assuming an integer result. sqrt(16) is a float. If you need an integer, for example to check for a perfect square, cast or round deliberately and compare carefully.
  3. Ignoring negative input. Data from users or calculations such as $b * $b - 4 * $a * $c can be negative, producing NAN that silently spreads through later arithmetic.
  4. Relying on printed precision. echo rounds according to the precision directive. Use round() or number_format() when the display format matters.
  5. Passing non-numeric strings. These raise a TypeError; validate input first.
  • pow() and the ** operator: raising to a power. A power of 0.5 gives the same value as a square root for non-negative numbers.
  • is_nan(): detecting the NAN result.
  • round() and number_format(): controlling decimal places.
  • M_SQRT2, M_SQRT3, M_SQRT1_2, M_SQRTPI and M_2_SQRTPI: predefined constants for common roots.

Recap

sqrt(float $num): float returns the square root of its argument as a float. Negative input gives NAN rather than an error, so check it beforehand or with is_nan(). Format results explicitly when output must be exact, and compare floats with a tolerance.

Tested examples

Basic square roots
<?php
echo sqrt(9), PHP_EOL;
echo sqrt(0), PHP_EOL;
echo sqrt(2.25), PHP_EOL;
Output
3
0
1.5
Guarding against negative input
<?php
function safeSqrt(float $n): ?float
{
    return $n < 0 ? null : sqrt($n);
}

foreach ([16, -1] as $n) {
    $r = safeSqrt($n);
    echo $r === null ? 'undefined' : $r, PHP_EOL;
}
Output
4
undefined
Distance between two points
<?php
function distance(float $x1, float $y1, float $x2, float $y2): float
{
    return sqrt(($x2 - $x1) ** 2 + ($y2 - $y1) ** 2);
}

echo distance(1, 2, 4, 6), PHP_EOL;
echo number_format(distance(0, 0, 1, 1), 4), PHP_EOL;
Output
5
1.4142