Signature
sqrt(float $num): floatPurpose
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.
echo sqrt(9), PHP_EOL;
echo sqrt(0), PHP_EOL;
echo sqrt(2.25), PHP_EOL;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.
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;
}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.
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;5 1.4142
Edge cases
- Zero:
sqrt(0)is0.0. - Negative numbers: the result is
NAN, with no exception. - Infinity: the square root of positive infinity is positive infinity, and
NANinput staysNAN. - 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
- Comparing with NAN directly.
$root === NANis always false. Useis_nan($root). - 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. - Ignoring negative input. Data from users or calculations such as
$b * $b - 4 * $a * $ccan be negative, producingNANthat silently spreads through later arithmetic. - Relying on printed precision.
echorounds according to theprecisiondirective. Useround()ornumber_format()when the display format matters. - Passing non-numeric strings. These raise a
TypeError; validate input first.
Related functions
pow()and the**operator: raising to a power. A power of0.5gives the same value as a square root for non-negative numbers.is_nan(): detecting theNANresult.round()andnumber_format(): controlling decimal places.M_SQRT2,M_SQRT3,M_SQRT1_2,M_SQRTPIandM_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
echo sqrt(9), PHP_EOL;
echo sqrt(0), PHP_EOL;
echo sqrt(2.25), PHP_EOL;
3 0 1.5
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;
}
4 undefined
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;
5 1.4142