Saturday , August 15 2020
Home » PHP & MySQL » Simple PHP Calculator
php-calculator code

Simple PHP Calculator

When I was a student of computer science, creating a php calculator code was one of interesting tasks I was assigned. It looks like most of  computer ‘s students go through this assignment either in college or initial semesters of university. I just reflected my experience and decided to write some code lines to make student’s life easy.

One can create a calculator with different logics and code segments. 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. This simple php calculator code will make the work

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>

<body >
<H3>Simple Calculator In PHP Using IF Statement</H3>
<table style=”width:30% “>
<form name=”calc” action=”” method=”POST”>
<th>Input Values</th>
<th>Select Operation</th>
<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>
<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>
<td><input type=submit name=”btn_calculate” value=”Calculate”></td>

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

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

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 understand php code to clear your concepts..

$number1 = $_POST['value1'];
$number2 = $_POST['value2'];
$oper = $_POST['oper'];
# Checking any value is entered in text boxes.
echo("You must enter number 1");
echo("You must enter number 2");
# Checking if any operation is selected .
echo("You must select an operation to do with the numbers!");
# Checking if values entered in text boxex are numeric values.
if(!eregi("[0-9]", $number1)){
echo("Number 1 MUST be Numeric Value!");
if(!eregi("[0-9]", $number2)){
echo("Number 2 MUST be Numeric Value!");
# 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");
case "SUBTRACT":
$result =$number1 - $number2;
echo("The difference of $number1 and $number2 is $result");
echo("$number1 - $number2 = $result");
case "DIVIDE":
$result =$number1 / $number2;
echo("The division of $number1 and $number2 is $result");
echo("$number1 / $number2 = $result");
case "MULTIPLY":
$result =$number1 * $number2;
echo("The product of $number1 and $number2 is $result");
echo("$number1 * $number2 = $result");
echo("No operation completed");

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.


  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.

  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!

  14. Thank you a bunch for sharing this with all folks you really recognise what you’re talking about! Bookmarked. Kindly additionally visit my site =). We may have a link trade contract between us!

  15. I just got done eating a platter of spaghetti before visiting your site. It sure makes the full feeling all that much better.

  16. hi!,I love your writing very a lot! share we keep up a correspondence more approximately your article on AOL? I require an expert in this space to solve my problem. Maybe that’s you! Having a look forward to see you.

  17. I think what you said made a lot of sense. But, consider this, what if you added a little content? I ain’t saying your content is not good., but what if you added something that makes people want more? I mean is kinda vanilla. You ought to glance at Yahoo’s front page and see how they create news titles to grab people to open the links. You might add a video or a pic or two to grab people excited about everything’ve written. In my opinion, it might make your blog a little bit more interesting.

  18. If some one desires to be updated with most recent technologies afterward
    he must be pay a visit this web site and be up to date daily.

  19. Great beat ! I would like to apprentice while you
    amend your site, how can i subscribe for a blog web
    site? The account aided me a acceptable deal. I had been a little bit
    acquainted of this your broadcast offered bright clear concept

  20. Howdy! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be book-marking and checking back frequently!

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

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

  23. With thanks! Valuable information!

  24. Black on black in the Charg I’m creepin’ Rub me the right way, you might get a genie B.o.B, black Houdini

  25. Wonderful article! That is the kind of information that are supposed to be
    shared across the web. Disgrace on the search engines for not positioning this submit upper!
    Come on over and discuss with my web site . Thanks =)

  26. I am genuinely thankful to the owner of this website who has shared this great paragraph at here.

  27. Ahaa, its fastidious dialogue about this article here
    at this webpage, I have read all that, so now me also commenting here.

  28. I like your writing style genuinely loving this internet site.

  29. I like the helpful information you supply for your articles.

    I will bookmark your blog and take a look at once more here regularly.
    I am reasonably sure I will learn many new stuff
    proper here! Good luck for the following!

  30. It is perfect time to make some plans for the future and
    it is time to be happy. I’ve read this post and if I could
    I wish to suggest you few interesting things or
    tips. Maybe you can write next articles referring to this article.

    I want to read even more things about it!

  31. This info is worth everyone’s attention. When can I
    find out more?

  32. It’s fantastic that you are getting thoughts from this article as well as from our discussion made here.

  33. Wow, marvelous blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your website is magnificent,
    as well as the content!

  34. You can certainly see your enthusiasm within the
    article you write. The world hopes for even more passionate writers such as you
    who aren’t afraid to say how they believe. Always
    go after your heart.

  35. Excellent article. I definitely appreciate this site.
    Keep writing!

  36. Piece of writing writing is also a excitement, if you be familiar with then you can write otherwise it is
    complicated to write.

  37. It’s actually a cool and useful piece of information. I am glad that you just
    shared this helpful info with us. Please keep us up to date like this.
    Thanks for sharing.

  38. autoliker, Facebook Liker, Increase Facebook Likes, Photo Auto Liker, Autoliker, Autoliker, Auto Like, Photo Liker, auto like, Fb Autoliker, autolike, Working Auto Liker, Status Auto Liker, Auto Liker, facebook auto liker, Facebook Autoliker, Autoliker Facebook, auto liker, Status Liker, Facebook Auto Liker

  39. I’ve been exploring for a bit for any high quality articles or blog
    posts on this sort of area . Exploring in Yahoo
    I ultimately stumbled upon this website. Studying this information So i’m glad to exhibit that I
    have a very just right uncanny feeling I discovered exactly what I needed.
    I most no doubt will make certain to do not forget this website and give it a look on a continuing basis.

  40. Hmm is anyone else experiencing problems with the pictures on this blog
    loading? I’m trying to find out if its a problem on my end or if it’s the blog.
    Any suggestions would be greatly appreciated.

  41. Hi there, I would like to subscribe for this web site to obtain most recent updates, thus where can i
    do it please help.

  42. you’re really a excellent webmaster. The site loading velocity is amazing.
    It seems that you’re doing any unique trick. Also, The contents are masterwork.
    you have performed a magnificent task on this matter!

  43. Thank you Rehmat Jan, there is so much to learn. I appreciate your expertise and sharing of all of these different tips. Great to learn PHP as for career.

  44. vlogger terfavorit Thanks a lot considerably to get spreading the following wonderful folks that you comprehend what you’re really discussing! Book-marked. You need to on top of that contact this site Equals). Organic beef employ a url business layout in between people

  45. Nice replies in return of this difficulty with genuine arguments and explaining all concerning that.|

  46. When someone writes an article he/she maintains the image of a user in his/her mind that
    how a user can know it. Therefore that’s why this piece of writing is
    great. Thanks!

  47. May I simply say what a relief to discover somebody that actually understands what they’re discussing online.
    You definitely know how to bring a problem to light
    and make it important. More people really need to check this
    out and understand this side of the story. I can’t believe
    you’re not more popular given that you definitely have the gift.

  48. I have been browsing on-line more than 3 hours lately, yet I by no
    means discovered any fascinating article like yours. It’s beautiful value sufficient
    for me. In my opinion, if all site owners and bloggers made just right content as you did, the web will probably be much more useful
    than ever before.

  49. Good blog post. I certainly appreciate this site.
    Stick with it!

  50. Hi, yeah this post is truly pleasant and I have learned lot of
    things from it concerning blogging. thanks.

  51. I’m extremely impressed with your writing skills as well as with the
    layout on your blog. Is this a paid theme or did you modify
    it yourself? Either way keep up the excellent quality writing, it’s rare to see a great blog like this one these days.

  52. Enjoyed the post.

  53. Every weekend i used to visit this web site, because i want enjoyment, as
    this this website conations in fact pleasant funny stuff too.

  54. wonderful points altogether, you simply gained a new reader. What would you recommend about your post that you made some days ago? Any positive?

  55. I am really loving the theme/design of your web site.
    Do you ever run into any internet browser compatibility issues?
    A few of my blog audience have complained about my site not working correctly in Explorer but looks great in Safari.
    Do you have any suggestions to help fix this problem?

  56. Write more, thats all I have to say. Literally, it seems as though you relied on the video to
    make your point. You obviously know what youre talking about,
    why waste your intelligence on just posting videos to your site when you could be giving
    us something informative to read?

  57. Why visitors still use to read news papers when in this technological world everything is existing on web?

  58. This design is incredible! You most certainly know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start my own blog
    (well, almost…HaHa!) Excellent job. I really loved
    what you had to say, and more than that, how you presented it.
    Too cool!

  59. Hi! I could have sworn I’ve been to this blog before but after
    checking through some of the post I realized it’s new to me.

    Nonetheless, I’m definitely happy I found it and I’ll be bookmarking and
    checking back often!

  60. I’ve observed that in the world the present moment, video games include the latest rage with children of all ages. Occasionally it may be out of the question to drag young kids away from the games. If you want the best of both worlds, there are lots of educational gaming activities for kids. Great post.

  61. I consider something really special in this site.

  62. I was wondering if you ever considered changing the page layout of
    your site? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so
    people could connect with it better. Youve got an awful lot of text
    for only having one or two images. Maybe you could space it out better?

  63. I’d like to find out more? I’d want to find
    out more details.

  64. Hi there, itѕ good paragraph about media print, ᴡe all be aware of media іs a great
    source of data.

  65. These are actually enormous ideas in on the topic of blogging.

    You have touched some good factors here. Any way keep up wrinting.

  66. Great write-up, I’m normal visitor of one’s site, maintain up the nice operate, and It is going to be a regular visitor for a long time.

  67. Hello, i feel that i saw you visited my site so i got
    here to go back the want?.I am attempting to to find issues to enhance my website!I guess its ok to use some of your ideas!!

  68. Your site has proven useful to me.

  69. This paragraph is genuinely a nice one it helps new web viewers,
    who are wishing in favor of blogging.

  70. Hello! I know this is kinda off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having trouble finding one? Thanks a lot!

  71. What I have seen in terms of laptop memory is the fact that there are specs such as SDRAM, DDR or anything else, that must match up the features of the mother board. If the computer’s motherboard is pretty current and there are no os issues, modernizing the ram literally requires under a couple of hours. It’s one of several easiest computer upgrade methods one can picture. Thanks for spreading your ideas.

  72. backlink data indexletme.

Leave a Reply to stem cells and penis enlargement Cancel reply

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