Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

sqrt

Returns the square root of a number.

Syntax

sqrt(float $num): float

Parameters

ParameterTypeDescription
$numfloatA non-negative number.

Return value

floatThe square root.

Examples

A square root:

<?php
 
echo sqrt(81), "\n";
echo round(sqrt(2), 3);

Output:

9
1.414

The distance between two points:

<?php
 
$dx = 3;
$dy = 4;
 
echo sqrt($dx ** 2 + $dy ** 2);

Output:

5

Notes

For a negative number the result is NAN, "not a number". Check for it with is_nan().

See also

  • pow — Raises a number to a power.
  • abs — Returns the absolute value of a number.

Related recipes

In the tutorial

  • Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.

Thanks for reading!

Donate to the site — we work hard for you.