
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP sqrt() Function
Definition and Usage
The sqrt() function returns square root of a positive float number. Since square root for negative number is not defined, it returns NAN. This is one of the most commonly used functions.
This function always returns a floating point number.
Syntax
sqrt ( float $arg ) : float
Parameters
Sr.No | Parameter & Description |
---|---|
1 | arg a number whose square root is to be obtained |
Return Values
PHP sqrt() function returns square root of the given arg number. For negative number, the function returns NAN.
PHP Version
This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.
Example
Following example calculate square root of 100−
<?php $arg = 100; echo "Square root of " . $arg . "=" . sqrt($arg) . "
"; ?>
Output
This will produce following result −
Square root of 100=10
Example
For sqrt(2), 1/sqrt(2) and sqrt(3), PHP has special predefined constants M_SQRT2, M_SQRT1_2 and M_SQRT3 respectively −
<?php echo "sqrt(2) = " . sqrt(2) . "
"; echo "M_SQRT2 = " . M_SQRT2. "
"; echo "sqrt(3) = " . sqrt(3) . "
"; echo "M_SQRT3 = " . M_SQRT3 . "
"; echo "1/sqrt(2)) = " . 1/sqrt(2) . "
"; echo "M_SQRT1_2 = " . M_SQRT1_2 . "
"; ?>
Output
This will produce following result −
sqrt(2) = 1.4142135623731 M_SQRT2 = 1.4142135623731 sqrt(3) = 1.7320508075689 M_SQRT3 = 1.7320508075689 1/sqrt(2)) = 0.70710678118655 M_SQRT1_2 = 0.70710678118655
Example
The mathematical constants M_SQRTPI and M_2_SQRTPI represent values of sqrt(Π) and 2/sqrt(Π) −
<?php echo "sqrt(pi) = " . sqrt(M_PI) . "
"; echo "M_SQRTPI = " . M_SQRTPI. "
"; echo "2/sqrt(pi) = " . 2/sqrt(M_PI) . "
"; echo "M_2_SQRTPI = " . M_2_SQRTPI . "
"; ?>
Output
This will produce following result −
sqrt(pi) = 1.7724538509055 M_SQRTPI = 1.7724538509055 2/sqrt(pi) = 1.1283791670955 M_2_SQRTPI = 1.1283791670955
Example
sqrt(-1) is undefined hence it returns NAN
<?php echo "sqrt(-1) = " . sqrt(-1) . "
"; ?>
Output
This will produce following result −
sqrt(-1) = NAN
- Related Articles
- sqrt() function in PHP
- PHP Function arguments
- PHP abs() Function
- PHP acos() Function
- PHP acosh() Function
- PHP asin() Function
- PHP asinh() Function
- PHP atan() Function
- PHP atan2() Function
- PHP atanh() Function
- PHP base_convert() Function
- PHP bindec() Function
- PHP ceil() Function
- PHP cos() Function
- PHP cosh() Function
