What is sqrt() in PHP?

The sqrt() function in PHP returns the square root of a non-negative number.

Figure 1 below shows the mathematical representation of the sqrt() function.

Figure 1: Mathematical representation of the sqrt() function

Syntax

float sqrt(number)

Parameter

sqrt() requires a non-negative number as a parameter. If the number is negative, then this function returns NaN.

Return value

sqrt() returns the square root of the number sent as a parameter.

Code

The following example shows how to use the sqrt() function in PHP.

<?php
echo("sqrt(4): ");
echo (sqrt(4));
echo("\n");
echo("sqrt(0): ");
echo (sqrt(0));
echo("\n");
echo("sqrt(25): ");
echo (sqrt(25));
echo("\n");
echo("sqrt(4.5): ");
echo (sqrt(4.5));
echo("\n");
#error output
echo("sqrt(-4): ");
echo (sqrt(-4));
?>

Free Resources