Site icon T4Tutorials.com

Scientific calculator source code in PHP with database 

Scientific calculator source code in PHP with database

This is a Scientific calculator that can perform the simple arithmetic tasks (+, -, * and /) as well as other math functions, such as, sin, cos, tan, min, max, log, exponent, pi, power, and square root.

HTML Code of Scientific calculator

<form method="post" action="">
<h1>Scientific Calculator</h1><br>
<p>*Please Enter Numbers in the First Field Only For Math Functions.</p>
1st Number: <input name="num1" value=""><br>
2nd Number: <input name="num2" value=""><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
<input type="submit" name="sub" value="x">
<input type="submit" name="sub" value="/"><br>
<input type="submit" name="sub" value="e">
<input type="submit" name="sub" value="pi">
<input type="submit" name="sub" value="pow">
<input type="submit" name="sub" value="sqrt"><br>
<input type="submit" name="sub" value="cos">
<input type="submit" name="sub" value="sin">
<input type="submit" name="sub" value="tan">
<input type="submit" name="sub" value="log"><br>
<input type="submit" name="sub" value="max">
<input type="submit" name="sub" value="min">
<br>Answer: <input type='text' value="<?php echo $ans; ?>"><br>
</form><div class="button">
<form method="post">
	<input class="get" type="submit" name="answer" value="Retrieve Data">
</form>

PHP Code of Scientific calculator

We are using Else-If statement for checking which operator to apply.

<?php
if(isset($_POST['sub']))
{
	$num1=$_POST['num1'];
	$num2=$_POST['num2'];
	$oprtr=$_POST['sub'];
	if($oprtr=="+")
		$ans=$num1+$num2;
	else if($oprtr=="-")
		$ans=$num1-$num2;
	else if($oprtr=="x")
		$ans=$num1*$num2;
	else if($oprtr=="/")
		$ans=$num1/$num2;
	else if($oprtr=="cos")
		$ans=cos($num1);
	else if($oprtr=="sin")
		$ans=sin($num1);
	else if($oprtr=="tan")
		$ans=tan($num1);
	else if($oprtr=="e")
		$ans=exp($num1);
	else if($oprtr=="log")
		$ans=log($num1);
	else if($oprtr=="pi")
		$ans=pi($num1);
	else if($oprtr=="pow")
		$ans=pow($num1);
	else if($oprtr=="sqrt")
		$ans=sqrt($num1);
	else if($oprtr=="max")
		$ans=max($num1,$num2);
	else if($oprtr=="min")
		$ans=min($num1,$num2);
}
?>

We are going to store the numbers, operators, and answers in the database.

Database Connection Code for Scientific calculator

Our database name is “calculator”. The table name is “valuescal”.

<?php
$conn = mysqli_connect("localhost","root","","calculator");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

Database of Scientific calculator

Let’s see the screenshot of the database of the scientific calculator.

Query:

To insert data in the database we will use this query:

$sql = "INSERT INTO `valuescal` (`id`, `number1`, `number2`, `operator`, `result`) VALUES (NULL, '$num1', '$num2', '$oprtr', '$ans') ";
	if ($conn->query($sql) === TRUE){
    echo "Answers and Values are Saved.";
    } 
    else {
    echo "Error whilst saving Answers and Values: " . $sql . "<br>" . $conn->error;
    }

To get the stored data from the database:

<?php
if(isset($_POST['answer']))
{
$sql = "SELECT * FROM valuescal";
$result = $conn->query($sql);
if(mysqli_num_rows($result) > 0){
		echo "<br><center><b>All Answers:<b></center><br>";
        echo "<center><table style='border: 2px dashed grey;'>";
            echo "<tr>";
                echo "<th>First Number:</th>";
                echo "<th>Second Number:</th>";
                echo "<th>Operator</th>";
                echo "<th>Result</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['number1'] . "</td>";
                echo "<td>" . $row['number2'] . "</td>";
                echo "<td>" . $row['operator'] . "</td>";
                echo "<td>" . $row['result'] . "</td>";
            echo "</tr>";
        }
        echo "</table></center>";
        mysqli_free_result($result);
    } else{
        echo "Don't have any saved data in the database.";
    }
}
?>

Output:
The output of Data retrieved:

Download Code – scientificcalculator

More Examples of Calculators

  1. Simple Calculator Using PHP
  2. Age calculator in PHP
  3. Scientific calculator in PHP with database
Exit mobile version