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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & 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.
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.
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:
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:
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:
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:
Output – 2: With 100 as input.
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:
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.
The above is the detailed content of Square Root in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
