Monday , May 21 2018
Home » PHP & MySQL » Simple PHP Calculator
php-calculator

Simple PHP Calculator

When I was a student of computer science, creating a php calculator code was one of interesting tasks I was assigned. One can create a calculator with different logic and segment of code. I am writing this post to help students of college and university to make easy understandable calculator in PHP.

Calculator code segments

I have used simple segments of HTML and PHP codes. The HTML is used for simple design and PHP segments describe the back-end logic and calculations. This code is tested and error free, let’s discuss the codes one by one in detail.

HTML code to design php calculator

The code given below contains two text boxes for entering a numeric values and four radio buttons to select an operator and finally a simple button. I presume you have installed XAMMP or WAAMP server on your computer which is necessary for running a server.

calculator in php

<TITLE>Calculator in PHP</TITLE>
<head>

</head>
<html>
<body >
<H3>Simple Calculator In PHP Using IF Statement</H3>
<table style=”width:30% “>
<form name=”calc” action=”” method=”POST”>
<tr>
<th>Labels</th>
<th>Input Values</th>
<th>Select Operation</th>
</tr>
<tr>
<td>Number 1:</td>
<td><input type=text name=”value1″></td>
<td><input type=radio name=”oper” value=”ADD”>Addition </input><br>
<input type=radio name=”oper” value=”SUBTRACT”>Subtraction</input></td>
</tr>
<tr>
<td>Number 2:</td>
<td><input type=text name=”value2″></td>
<td><input type=radio name=”oper” value=”DIVIDE”>Division </input><br>
<input type=radio name=”oper” value=”MULTIPLY”>Multiplication</input></td>
</tr>
<tr>
<td><input type=submit name=”btn_calculate” value=”Calculate”></td>
</tr>
</form>
</table>
</body>
</html>

PHP code for calculator using if statement

This segment of php code is designed to take values from text boxes and apply operation selected. eregi is useful to assure the values in text boxes are numeric not alphabetic.  There are four option given to user to select operation. If statement is used to check the operator and simple expression used for Add, Subtract, Divide and Multiply. So this php calculator code can do basic four operations. Just paste following code after closing html tag

<?php
if(isset($_POST['btn_calculate'])){
$number1 = $_POST['value1'];
$number2 = $_POST['value2'];
$oper = $_POST['oper'];
if(!$number1){
echo("You must enter number 1");
exit;
}
if(!$number2){
echo("You must enter number 2");
exit;
}
if(!$oper){
echo("You must select an operation to do with the numbers!");
exit;
}
if(!eregi("[0-9]", $number1)){
echo("Number 1 MUST be numbers!");
exit;
}
if(!eregi("[0-9]", $number2)){
echo("Number 2 MUST be numbers!");
exit;
}
if($oper == "ADD"){
$result =$number1 + $number2;
echo("The sum of $number1 and $number2 is $result<br><br>");
echo("$number1 + $number2 = $result");
exit;
}
if($oper == "SUBTRACT"){
$result =$number1 - $number2;
echo("The difference of $number1 and $number2 is $result<br><br>");
echo("$number1 - $number2 = $result");
exit;
}
if($oper == "DIVIDE"){
$result =$number1 / $number2;
echo("The division of $number1 and $number2 is $result<br><br>");
echo("$number1 / $number2 = $result");
exit;
}
if($oper == "MULTIPLY"){
$result =$number1 * $number2;
echo("The product of $number1 and $number2 is $result<br><br>");
echo("$number1 * $number2 = $result");
exit;
}
}
?>

PHP code for calculator using switch statement

This segment of php code is designed to calculate same operations i.e Addition, Subtraction, Multiplication and Division. Almost same as above segment do but here demonstrated usage of switch statement as alternative of IF ELSE statement. We tried to make it easy for students in understanding concepts of both statements. HTML code is same as above so here need to have a look on php code.


if(isset($_POST['btn_calculate'])){
$number1 = $_POST['value1'];
$number2 = $_POST['value2'];
$oper = $_POST['oper'];
# Checking any value is entered in text boxes.
if(!$number1){
echo("You must enter number 1");
exit;
}
if(!$number2){
echo("You must enter number 2");
exit;
}
# Checking if any operation is selected .
if(!$oper){
echo("You must select an operation to do with the numbers!");
exit;
}
# Checking if values entered in text boxex are numeric values.
if(!eregi("[0-9]", $number1)){
echo("Number 1 MUST be Numeric Value!");
exit;
}
if(!eregi("[0-9]", $number2)){
echo("Number 2 MUST be Numeric Value!");
exit;
}
# Checking each operators and performing required operation using Switch statement.
switch ($oper) {
case "ADD":
$result =$number1 + $number2;
echo("The sum of $number1 and $number2 is $result");
echo("$number1 + $number2 = $result");
break;
case "SUBTRACT":
$result =$number1 - $number2;
echo("The difference of $number1 and $number2 is $result");
echo("$number1 - $number2 = $result");
break;
case "DIVIDE":
$result =$number1 / $number2;
echo("The division of $number1 and $number2 is $result");
echo("$number1 / $number2 = $result");
break;
case "MULTIPLY":
$result =$number1 * $number2;
echo("The product of $number1 and $number2 is $result");
echo("$number1 * $number2 = $result");
break;
default:
echo("No operation completed");
break;
}
}
?>

php calculator code
A simple php calculator using switch statement

We hope this HTML and PHP code segments would help you, keep visiting for more posts.

About Rehmat Jan

Rehmat Jan is a Web Developer, SEO Expert, Founder of Learn Blogger Tricks . Always ready for new experiences and leanings.

17 comments

  1. Your blog would increase in ranking if you post more often.`,;:”

  2. Very well explained , very good job. loved this segment of code its error free…

  3. Helpful and simple code segment. Thanks for share

  4. Simple and well explained code segment. Thanks for share

  5. Just what I was looking for! Thanks for the HTML code. This has been very helpful.

  6. I remember my brother writing HTML codes when he was doing his BCA. He often talked about C and C++, and then Java. Internet was not much popular during that time and only books were his source. But now it’s different. Students can really learn simple coding from this article. Indeed, a useful post.

  7. Making money online is getting harder and harder but with this little known trick you can start earning almost immediately. This actually works I have tried and tested the method myself. See for yourself. http://google-sniper.product-reviews.online/

  8. Thank you so much for the great article, it was fluent and to the point. Cheers.

  9. I like the valuable info you provide for your articles. I will bookmark your weblog and test once more here frequently. I am fairly certain I will be informed plenty of new stuff right here! Best of luck for the following!

  10. There are some fascinating time limits in this article however I don’t know if I see all of them center to heart. There’s some validity but I’ll take hold opinion until I look into it further. Good article , thanks and we would like extra! Added to FeedBurner as properly

  11. whoah this blog is great i really like reading your posts. Keep up the great paintings! You realize, a lot of people are searching around for this information, you could help them greatly.

  12. You made some good points there. I looked on the internet for the subject and found most guys will approve with your blog.

  13. I recently realized your website the other day and that i happen to be following it’s routinely. You’ve got great deal of tips proper here so i delight in your lifestyle of online site likewise. Preserve acknowledge that there are succeed!

Leave a Reply

Your email address will not be published. Required fields are marked *