sqrt
Returns the square root of a number.
Syntax
sqrt(float $num): floatParameters
| Parameter | Type | Description |
|---|---|---|
| $num | float | A 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.414The distance between two points:
<?php
$dx = 3;
$dy = 4;
echo sqrt($dx ** 2 + $dy ** 2);Output:
5Notes
For a negative number the result is NAN, "not a number". Check for it with is_nan().
See also
Related recipes
In the tutorial
- Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.