Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP sqrt() Function
The sqrt() function returns the square root of a positive float number. Since square root for negative numbers is not defined, it returns NAN (Not a Number). This is one of the most commonly used mathematical functions in PHP.
This function always returns a floating point number.
Syntax
sqrt ( float $arg ) : float
Parameters
| Parameter | Description |
|---|---|
| arg | A number whose square root is to be obtained |
Return Values
PHP sqrt() function returns the square root of the given arg number. For negative numbers, the function returns NAN.
Examples
Basic Usage
Here's a basic example showing square root calculation −
<?php
echo "sqrt(16) = " . sqrt(16) . "<br>";
echo "sqrt(25) = " . sqrt(25) . "<br>";
echo "sqrt(2) = " . sqrt(2) . "<br>";
?>
sqrt(16) = 4 sqrt(25) = 5 sqrt(2) = 1.4142135623731
Negative Number Example
When dealing with negative numbers, sqrt() returns NAN −
<?php
echo "sqrt(-1) = " . sqrt(-1) . "<br>";
echo "sqrt(-25) = " . sqrt(-25) . "<br>";
?>
sqrt(-1) = NAN sqrt(-25) = NAN
Working with Variables
You can also use variables with the sqrt() function −
<?php
$num1 = 64;
$num2 = 100;
echo "Square root of $num1 = " . sqrt($num1) . "<br>";
echo "Square root of $num2 = " . sqrt($num2) . "<br>";
?>
Square root of 64 = 8 Square root of 100 = 10
Conclusion
The sqrt() function is essential for mathematical calculations in PHP. It works with positive numbers and returns NAN for negative values, making it reliable for basic square root operations.
