EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Square Root in PHP

Home » Software Development » Software Development Tutorials » PHP Tutorial » Square Root in PHP

Square Root in PHP

Introduction to Square Root in PHP

Calculating other roots like the nth root of a number, or cube root of a number, similarly, we need to find the square root of numbers in PHP. We calculate these roots by using different functions like pow(), log() and others.

In a programming language like PHP, calculating square root is simple when used with built-in function. This function is sqrt(). We will also see how to find the square root of a number without using sqrt() and how to calculate square root using a form with user input.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The sqrt() function is used to calculate the square root of a given number. This function is a built-in Math function used in PHP like pow(), rand(), is_nan() etc.

Square Root Logic

The syntax and description of square root logic is explained in details below,

Syntax:

sqrt($num)

Where $num is the single argument passed to the sqrt function.

Description: sqrt() function calculates and returns the square root of the given number. The returned value is of type float. Also, we have different types of input numbers to the given function on which the square root function is performed and the result is calculated.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Here we will see that the input numbers can be positive or negative numbers or decimal numbers (float) or it can also be zero. The positive numbers return positive numbers as output and negative numbers return NAN (Not a Number) as output, the square root of decimal numbers is a float as output, and the square root of one is one. Also, remember the square root of zero is zero.

Finding Square Root of a Given Number

The square root of a given number is as per the following,

If the input number is 81, the square root of the number will be 9. If the input number is 49, the square root number will be 7 and so on.

Let us learn this with an example:

We will also learn to find the square root with different types of input.

Example #1

Code:

<?php
// simple example to find how sqrt() function works on numbers
echo sqrt(16);
echo '<br>';
// output is 4
echo sqrt(7);
echo '<br>';
//output is 2.6457513110646
?>

Output:

sqrt() in PHP

In the above program, the output is 4 as we know 4*4 is 16 thus the square root of 16 is 4. While calculating the square root of 7, we see that after the decimal many digits are found, the number of digits after the decimal depends upon the user.

Similar to the sqrt function, which calculates the square root of the given number. To calculate any root of the given number we use pow() function which stands for power.

Example #2

Code :

<?php
// example to calculate any root
echo '<br>'.'Result of  :   pow(16, 1/2)  ======  '. pow(16, 1/2);
// example to calculate the cube root of 27
echo '<br>'.'Result of  : pow(27, 1/3)  ======  '. pow(27, 1/3);
//example to calculate the fourth root of 12
echo '<br>'.'Result of  : pow(12, 1/4)  ======  '. pow(12, 1/4);
//example to calculate the fifth root of 76
echo '<br>'.'Result of  : pow(76, 1/5)  ======  '. pow(76, 1/5);
//example to calculate the sixth root of 88
echo '<br>'.'Result of  : pow(88, 1/6)  ======  '. pow(88, 1/6);
?>

Output:

pow() function

Example #3

Code:

<?php
echo '<br>'.'Result of  :   sqrt(625)  ======  '. sqrt(625);
echo '<br>'.'Result of  :   sqrt(49)  ======  '. sqrt(49);
echo '<br>'.'Result of  :   sqrt(-36)  ======  '. sqrt(-36);
echo '<br>'.'Result of  :   sqrt(0)  ======  '. sqrt(0);
echo '<br>'.'Result of  :   sqrt(121)  ======  '. sqrt(121);
echo '<br>'.'Result of  :   sqrt(22)  ======  '. sqrt(22);
echo '<br>'.'Result of  :   sqrt(12.34)  ======  '. sqrt(12.34);
echo '<br>'.'Result of  :   sqrt(-16)  ======  '. sqrt(-16);
?>

Output:

Square Root in PHP-1.3

Example #4

Finding Square Root of A Number Entered by The User Through a Form: In the following program, we have created a program in PHP to calculate the square root of a number entered by the user through a form. Suppose the user has entered 16 then we can find the square root of the 16 and expect the result as 4, if the user entered 49 we can expect the result as 7 and so on.

Also, we have used the built-in mathematical function sqrt() to find the square root.

Code:

<!---program to calculate square root of input number using form -->
<html>
<head>
<title>Square root of a number using form</title>
</head>
<body>
<!--- input form with text box --->
<form method="post" action="">
<label>Enter a number</label>
<input type="text" name="input" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])) {
//storing the number in a variable $input
$input = $_POST['input'];
//storing the square root of the number in a variable $ans
$ans = sqrt($input);
//printing the result
echo 'The square root of '.$input.'====='.$ans;
}
?>
</body>
</html>

Output – 1:

Square Root in PHP-1.4

Output – 2: With 100 as input.

Square Root in PHP-1.5

Example #5

Finding Square Root of A Number without Using Built-in sqrt() Function: In the following program, we have created a program in PHP to calculate the square root of a number without using built-in sqrt() function.

Code:

function squareroot($input)
{
//if the input number is 0 then return 0 as result
if($input == 0) {
return 0;
}
//if the input number is 1 then return 1 as result
if($input == 1) {
return 1;
}
// assigning $input value to a variable $a
$a = $input;
$b = 1;
while($a > $b)
{
// calculating the middle number
$a= ($a + $b)/2;
// dividing the input number with the middle number
$b = $input/$a;
}
return $a;
}
echo '<br>'.'Square root of 0 is '.squareroot(0);
echo '<br>'.'Square root of 20 is '.squareroot(20);
echo '<br>'.'Square root of 49 is '.squareroot(49);
echo '<br>'.'Square root of 81 is '.squareroot(81);
echo '<br>'.'Square root of 1 is '.squareroot(1);

Output:

Square Root in PHP-1.6

Conclusion

In this article, we learned what square root is, how do we calculate square roots with and without the built-in functions like sqrt(), pow(). What the sqrt() and pow() function does, how is it used in a program to find the square root? We learned about performing square root on numbers, floating-point numbers, negative numbers and so on. We also learned about calculating square root with user-defined input using form.

Recommended Articles

This is a guide to Square Root in PHP. Here we discuss the square root logic and finding the square root with different types of input and its examples. You may also look at the following articles to learn more –

  1. Patterns in PHP
  2. Variables in PHP
  3. How to Connect Database to PHP?
  4. Socket Programming in PHP

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you