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));
?>
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources