Site icon T4Tutorials.com

Square Root of A Number in PHP with form and database

Square Root of A Number in PHP with form and database

In this tutorial, we will learn about the followings.

  1. Square Root of A Number in PHP 
  2. Square Root of A Number in PHP with form 
  3. Square Root of A Number in PHP with database

Square Root of A Number in PHP.

In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.

<html>
<body>
<?php
$number=9;
$sr=sqrt($number);
echo"<h2>Square root of $number is $sr";
?>
</body>
</html>
 

Square Root of A Number in PHP with form.

In this example, we will take the inputs from the user and store them in variables.

<html>
<body>
<form method="post">
<p>Enter any number</p>
<input type="text" name="number" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit'])){
$number=$_POST['number'];
$sr=sqrt($number);

echo"Square root of $number is $sr</h2>";
}
?>
</body>
</html>

Square Root of A Number in PHP with the database.

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<html>
<body>
<form method="post">
<p>Enter any number</p>
<input type="text" name="number" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit'])){
$number=$_POST['number'];
$sr=sqrt($number);
echo"<h2>Square root of $number is $sr";
$con=mysqli_connect("localhost","root","","SquareRoot");
$sql=mysqli_query($con,"INSERT INTO t1 (number,sqrt) VALUES ('$number','$sr')");
}
?>
</body>
</html>

Database for the Program of Square Root of A Number.

Figure: squareRoot program in PHP database form.

 

Exit mobile version