If you have just started programming, you usually do a squaring operation without using a function. However, it is not easy to do this work without using the function when the square is the square root of the number of jobs.
However if aim is to calculate the square root of a number, you usually uses a function. Here is in this post I’ll show you How to calculate a square root without using method or function in PHP.
I would like to specify the name of the current function in PHP before running the following code sqrt is the function used in javascript to get the square root.
The following code shows how to get the square root with PHP without using the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php /*create custom function*/ function squareRoot($number){ if ($number > 0) { $root = $number / 3; for ($i = 0; $i < 32; $i++) $root = ($root + $number / $root) / 2; return $root; } else { return NAN; } } //output echo squareRoot(4) . "<br>"; echo squareRoot(25) . "<br>"; echo squareRoot("Mark") . "<br>"; |
Output: