How can we create a PHP Calculator? By very less coding & following some steps, we can create a basic calculator in PHP. This tutorial is for those people who want to learn PHP & beginner in PHP. If you have good knowledge in PHP you can create this program in less then 10 minutes.
Previously I had shared how to make a calculator with JavaScript? Now at this time, I am sharing the same type of calculator in the PHP platform. Basically, I created this program in PHP If { }
statement. The If { }
statement of any programming language work like either-or. Know more about PHP If statement.
I create this program in PHP & Bootstrap. I used bootstrap because I am lazy little bit, Using bootstrap I saved my almost 2-3 minute. First, I had created a form with method="POST"
then I added action with the same name of the program file. I had saved this program named calculator.php So, I had placed action="calculator.php"
in the form tag. Example: <form method="post" action="calculator.php">
Then I had created two text input and one option menu using HTML or Bootstrap. Gave the first input name ‘number1‘ and for the second input ‘number2‘. Example: <input name="number1" type="text" />
& gave name for selection section. Example: <select name="operation">
Then gave value for options (i.e. plus, minus ) & submit button. Example: <option value="plus">
& <input name="submit" type="submit" value="Calculate" />
Preview of Calculator Program
Let’s see a preview of this calculator in GIF version. In other words, see how this calculator works.
You May Also Like:
Source Code Of Simple PHP Calculator Program.
Now here I am sharing the source code of this basic PHP calculator, Who was still talking about. This is a very small program So, I created just one file for this. I am sure you will fully understand the whole program just looking at source code. Because this code is so simple & clean.
Create a file name ‘calculator.php‘ and put these following code given here below. Just Copy & Paste.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<!DOCTYPE html> <!-- code by webdevtrick ( https://webdevtrick.com) --> <html> <head> <title>Simple Calculator In PHP | Webdevtrick.com</title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container" style="margin-top: 50px"> <?php // If the submit button has been pressed if(isset($_POST['submit'])) { // Check number values if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])) { // Calculate total if($_POST['operation'] == 'plus') { $total = $_POST['number1'] + $_POST['number2']; } if($_POST['operation'] == 'minus') { $total = $_POST['number1'] - $_POST['number2']; } if($_POST['operation'] == 'multiply') { $total = $_POST['number1'] * $_POST['number2']; } if($_POST['operation'] == 'divided by') { $total = $_POST['number1'] / $_POST['number2']; } // Print total to the browser echo "<h1>{$_POST['number1']} {$_POST['operation']} {$_POST['number2']} equals {$total}</h1>"; } else { // Print error message to the browser echo 'Numeric values are required'; } } // end PHP. Code by webdevtrick.com ?> <!-- Calculator form by webdevtrick.com --> <form method="post" action="calculator.php"> <input name="number1" type="text" class="form-control" style="width: 150px; display: inline" /> <select name="operation"> <option value="plus">Plus</option> <option value="minus">Minus</option> <option value="multiply">Multiply</option> <option value="divided by">Devide</option> </select> <input name="number2" type="text" class="form-control" style="width: 150px; display: inline" /> <input name="submit" type="submit" value="Calculate" class="btn btn-primary" /> </form> </div> </body> </html> |
Now you have successfully created this program. now time to understand this program, look at the code you will definitely understand. I also placed a comment on every section of codes. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
It’s a pity you don’t have a donate button! I’d certainly donate to
this superb blog! I suppose for now i’ll settle for bookmarking and adding your RSS
feed to my Google account. I look forward to new updates and will talk about this blog with my Facebook group.
Chat soon!
thank you very much
Howdy would you mind letting me know which webhost you’re utilizing?
I’ve loaded your blog in 3 completely different web browsers and I
must say this blog loads a lot faster then most.
Can you recommend a good internet hosting provider at a fair price?
Cheers, I appreciate it!
bro i use a very cheap hosting service. 600 Indian rupees per year. I have just optimized
if you want to know about what hosting I use. reply this comment
Hello there! Would you mind if I share your blog with my myspace
group? There’s a lot of people that I think would really appreciate
your content. Please let me know. Many thanks
sure! why not? thank you very much
Pretty nice post. I just stumbled upon your weblog and wished
to mention that I’ve really enjoyed browsing your weblog posts.
After all I will be subscribing in your rss feed and I’m
hoping you write again very soon!
thank you very much, buddy.
Hello, this weekend is pleasant designed for me, since this time i am reading this fantastic educational piece of writing here at my
home.
thank you
Howdy! This is my 1st comment here so I just wanted to give a quick shout out and say I
really enjoy reading your articles. Can you suggest any other
blogs/websites/forums that cover the same subjects? Thanks a ton!
thank you very much. Really i don’t know any website or blog who provide simple web programs. they provide a very advanced program, which is difficult understand for beginners.
I like the valuable information you provide to your articles.
I will bookmark your weblog and test once more right
here frequently. I am slightly certain I’ll be told a lot of new stuff right here!
Good luck for the next!
thank you
Hi there! Would you mind if I share your blog with my myspace group?
There’s a lot of people that I think would really appreciate your content.
Please let me know. Cheers
This is matter of great pleasure to me. thank you
slot game
roulette for fun
gambling sites
blackjack game
casino games
Sorry, I didn’t get it.
please i am a beginner and i just copied the code to my visual studio.. please how do i see the project.. it cant be opened in the browser.. thaanks
Install Xampp and run apache and mySQL. download link – https://www.apachefriends.org/download.html
It’s a shame you don’t have a donate button! I’d without a doubt donate to this excellent blog!
I guess for now i’ll settle for book-marking
and adding your RSS feed to my Google account.
I look forward to new updates and will share this website with my Facebook group.
Chat soon!
Thank you very much, stay connected.
Can you do a calculator that can do a dynamic result for options
THIS IS NOT WORKING MY TEACHER GONNA KILL ME OMG NONONONONO!!!!!!!!!!!!!!!!!
i need assistance in adding some features on my website( PHP)