Best way to Create a Calculator in PHP with Code

Hey friends! In this PHP tutorial we gonna to be learning very simple Calculator in PHP. I am also providing the code of calculator in this article. You can do it very easily by practicing in PHP by using the function of arithmetic operation. We need a database in MySQl and a local host server, I recommend the best XAMPP. You can download this software by searching in Google or by clinking on this link. PHP language is very easy to use especially for programming where we and you can learn and develop big and small applications for all kind of use. And I am teaching you one of these so let’s create a simple Calculator in PHP with the help of video and Code.

Calculator in PHP with code
This is Picture of Calculator in this article.

How to create a Calculator in PHP using the Code?

  • Open your notepad or notepad++ and create a new file give it name “calculator.php”
  • Now save the file and give the name above mentioned
  • Now paste the below code into the file which you created and saved


<!DOCTYPE HTML>
<html lang=”en-US”>
<head>
<meta charset=”UTF-8″>
<title>Simple Calculator</title>
</head>
<style type=”text/css”>
table{
width:60%; background:#ff6666; margin:0 auto; box-shadow:10px 10px 30px #ccc; border-bottom:20px solid #ff6666;
}
.result-box{
width:60%; margin:0 auto; background: #ff6666; height:100px; margin-top:20px; padding:20px; color:white;box-shadow:10px 10px 30px #ccc; font-family:verdana;
}
td{
font-size:20px; padding:10px; font-family:verdana; color:#fff;
}
input[type=text]{
padding:10px; width:100px; font-size:20px;
}
input[type=submit]{
padding:10px; font-size:16px; cursor:pointer;border:none; background:#4799eb; color:#333; width:300px; height:50px;
}
input[type=reset]{
padding:10px; font-size:16px;cursor:pointer; border:none;background:#4799eb; color:#333; height:50px;
}
input[type=submit]:hover{
background:#eee; color:#444;
}
input[type=reset]:hover{
background:#eee; color:#444;
}
h1{
background:#ff6666; color:white; font-family:verdana; padding:20px;
}

</style>
<body>
<form action=”” method=”post”>
<table>

<center>
<h1>How To Create Simple Calculator Using PHP</h1>
<tr><td>Select First Value</td><td><input type=”text” name=”first_value” id=”” /></td></tr>
<tr><td>Select Second Value</td><td><input type=”text” name=”second_value” id=”” /></td></tr>
<tr><td>Select Oprator </td>
<td>
<select name=”operator” id=””>
<option value=”static”>Select Operator</option>
<option value=”+”>Addition</option>
<option value=”-“>Substraction</option>
<option value=”*”>Multiplication</option>
<option value=”/”>Division</option>
</select></td></tr>

<tr><td><input type=”reset” value=”Reset The Numbers” /></td><td><input type=”submit” name=”submit” value=”Let’s Calculate” /></td></tr>
<tr><td colspan=”2″>Author : www.yourwebschool.com</td></tr>
</center>
</table>
</form>

<?php
if(isset($_POST[‘submit’])){
$first_value = $_POST[‘first_value’];
$second_value = $_POST[‘second_value’];
$operator = $_POST[‘operator’];

?>
<div class=”result-box”>
<h3>Your Results : <?php
switch($operator){ case “+”:
echo $first_value + $second_value;
break;
case “-“:
echo $first_value – $second_value;
break;
case “*”:
echo $first_value * $second_value;
break;
case “/”:
echo $first_value / $second_value;
break;
default:
echo “Please Use Only Numbers”;
break;

}
}
?></h3>
</div>
</body>
</html>


The above simple PHP and HTML code will display the simple calculator with buttons and results in your browser with four arithmetic mathematic functions. Where you can put some values and after that can select the function which you want to apply and easily calculate and get Results by clicking on the button “Let’s calculate”.

Creating a Calculator in PHP Code full Video Tutorial


I hope after watching the Video tutorial, you got my point and understands that how it works easily. You design the buttons according to your styles. Change the color of buttons and resize them by editing in CSS and HTLL codes. We can also use this as online Currency convertor same syntax similar to this.

If you need any kind of help, any question in your mind, then inform us by using the below commenting box. We will always try to help your, you will always receive the positive response, your opinions are valuable for us. In future we will bring some great suggestions and some great ideas for you.

Share this information with your friends.