15: Exercises using PHP: Let’s build a calculator | PHP tutorial | Learn PHP programming

Exercises using PHP: Let’s build a calculator – Learn PHP backend programming. In this episode we will do a short exercise where we use some of the knowledge we have gathered from previous episodes. The primary reason behind this exercise, is to show how we can use what we have learned so far, to build an actual PHP application on our website.

mmtuts is a YouTube channel that focuses on teaching beginner and advanced courses in various multimedia related skills.
We plan to make tutorials available on programming, video production, animation, graphic design, and on software such as the Adobe Creative Cloud programs.

PHP for beginners is a how to series that teaches the PHP coding language to people who are just starting out learning programming. The course teaches how PHP scripting can be made easy and teaches how to build many apps such as a login system, a comment section, how to upload images, how to create users in a website, and much more. Creating dynamic websites with PHP is easy and should not be seen as otherwise, which is why we want to explain the language in a easy to understand way for beginners.

If you have suggestions on new courses, or specific lessons within existing courses you would like to see, then feel welcome to submit them in the comment section or in a private message. ALL suggestions will be seen, but not all will be replied to since we get quite a few every day.

Comments

Rems Nalivaiko says:

Can you tell me why it doesn’t work??

Here’s the code:

< !DOCTYPE html>



Calculator via PHP




Result ==>

< ?php if (isset($_GET['submit'])) { $result1 = $_GET['num1'] $result2 = $_GET['num2'] $operator = $_GET['operator'] switch ($operator) { case "Select..." : echo "you need to selec an a method"; break; case "Add" : echo $result1 + $result2; break; case "Substract" : echo $result1 - $result2; break; case "Multiply" : echo $result1 * $result2; break; case "Divide" : echo $result1 / $result2; break; } } ?>

Kevin Rodriguez says:

Could this method of creating a calculator be done in javascirpt?

GamesAway says:

ur color scheme name?

ShisHiR says:

can u please explain the button’s code part

like: what’s the use of “value” or “type” here?

Umair Mukhtar says:

comprehensive teaching. thank you sir

birdman says:

Another solid lesson! Thank you:)

Juicefulll says:

u sir are amazing

Karan Malhotra says:

After entering the values, the num1 and num2 field loads in the URL, but the operator field doesn’t. Please let me know the error in the code.

< !DOCTYPE html>



php lessons






The answer is:

< ?php if (isset($_GET['submit'])) { $result1 = $_GET['num1']; $result2 = $_GET['num2']; $operator = $_GET['operator']; switch ($operator) { case "None": echo "You need to select a method!"; break; case "Add": echo $result1 + $result2; break; case 'Subtract': echo $result1 - $result2; break; case 'Multiply': echo $result1 * $result2; break; case 'Divide': echo $result1 / $result2; break; default: echo "Hey"; break; } } ?>

ScreenPrintR says:

mmututs, thank you very much for your tutorials. I plan on watching all 63/64 of them. I especially like that fact that you show application programs and not just the basic programming. Thank you again.

Jvnm Chaitanya says:

Awesome videos, what packages do you use with ST3 for php and what is the colour scheme ?

Kin Chung CKC Chui says:

Thanks Daniel Sir. I have been watching (and giving likes) your tutorials from HTML to CSS, and here comes the PHP. You are just amazing.

One question. I follow your procedures to write the calculator. Everything seems right, just when i clicked the “Calculate” button, it said “404 Not found. The requested URL /index.html was not found on this server.”

What could possibly be the reason?

p.s. im using WAMP for the localhost since i cant start the Apache in XAMPP even i have no skype.

Thank you again.

Pawan Chhabra says:

How to set tax rate on ORPAT OT-1400T

Alex Yap says:

i’ve watched tons of PHP tutorials in the past but none of them are as comprehensive and as clear as yours, thank you so much and oh btw im no expert but just a quick suggestion… i think it’d help tons if you could show us a way how to check if the inputted entries are integers/floats and not strings so as to error-proof the application, but nevertheless superb tutorial! i subscribed

Philcob Josol says:

I get this errors.

Notice: Use of undefined constant Add – assumed ‘Add’ in C:xampphtdocscalcuindex.php on line 27

Notice: Use of undefined constant result1 – assumed ‘result1’ in C:xampphtdocscalcuindex.php on line 28

Notice: Use of undefined constant result2 – assumed ‘result2’ in C:xampphtdocscalcuindex.php on line 28
0

siddhant pandey says:

I m the only one here who is getting nothing after filling up the values

sharifa bahar says:

Thank you so much . It was really helpful . Your videos make me to survive in computer science . All the best

Tharindu Senevirathna says:

you teach me the PHP . Best php tutorial ever

Funky Attitude says:

Hey man, thank you for this tutorial but I have a question. In the form you did not actually specify the method u r going to use ( POST or GET ) and then in the php code you just used get, I assume if u don’t specify u can choose which one to use :S ?

Eric Lunsford says:

I have been working on a gross pay and over time calculator, however I cant seem to figure out how to make this PHP code work. Could you please help with the following
Create a website to calculate gross pay given
the number of hours and a pay rate. If the
number of hours is less than or equal to 40
the employee we will be paid at a normal rate.
If the number of hours is greater than 40 the
employee will be paid at time and a half for
the number of hours above 40.
Thank you

Shoaib Akhtar says:

have done it with bootstrap, happy to find results:
< !DOCTYPE html>




Calculator






< ?php if (isset($_GET['submit'])) { $result1 = $_GET['num1']; $result2 = $_GET['num2']; if ($result1 == "" && $result2 == "") { echo ("


“);
} elseif ($result1 == “” && $result2 != “”) {
echo (”


“);
} elseif ($result1 != “” && $result2 == “”) {
echo (”


“);
}

$operator = $_GET[‘operator’];
if ($result1 != “” && $result2 != “”) {
switch ($operator) {
case ‘None’:
echo (”


“);
break;
case ‘Add’:
$ans = $result1 + $result2;
echo (”


“);
break;
case ‘Subtract’:
$ans = $result1 – $result2;
echo (”


“);
break;
case ‘Multiply’:
$ans = $result1 * $result2;
echo (”


“);
break;
case ‘Divide’:
$ans = $result1 / $result2;
echo (”


“);
break;
}
}
}
?>





travioo says:

great work! Your tutorials are very helpful. Thanks!

Malebolgia of Kaasila City says:

really good tutorials, all of these. you make a fine teacher

Farhan Awan says:

is there any possibility that your php version and mine may be different . Becasue i wrote 100% same to same code but my one is not calculating . i think php code is not running

Reshma Kodwani says:

plz add more kinda exercise

Velibor Stojkovic says:

@mmtuts u are awesome man ty for this lesons again i learn more from u in this lesons and i read php book u help me so much 😀 just one question “is it Mysql database good (best) for php ?

Harold Wright says:

forms are annoying the hell out of me, so thanks for this!

Jose Angel says:

Great tutorial!

jaxall07 says:

Hey, so it’s not required to set the form method to GET?

. Other tutorials say setting this is a must, but I see you didn’t so I’m just clarifying some of my confusion lol. Thanks for the tutorial!
kaku sahi says:

keep getting error for operator varible,
Notice: Undefined index: operartor.
my code looks exactly like yours not sure wy its giving me error

WAVES Lin says:

hi can I ask how to limit user to just enter number, what is the method.

Frederik Volkers says:

Im getting an error, i would preciate if you can fix it;
Notice: Undefined index: operator






the answer is:

Amar Fila says:

Why I didnt find you earlier :(

Asif Ali says:

The Best Php Tutorials Ever

Sunny Dindoyal says:

excellent tutorial. what does the value=”submit” do in the button?

Andre S says:

Can you tell meu how to set num1 and num2 to be default null because when i select Add or Multiply etc and i don’t write nothing in the Number fields and i hit Calculate i get error. I need to force the user to enter some value in num1 and num2 before he hits “Calculate”

درس خفيف lightuts says:

you are the best

Philcob Josol says:

This works even if you didn’t put method=”GET” on the

tag?
amirtha rani says:

it’s not a calculator

aFluxx says:

Would be awesome if we could get the written php files in pastebin. So we can copy them and try em out ourselves.

Bilal Hameed says:

< !DOCTYPE HTML>







The answer is :

< ?php if (isset($_GET['submit'])) { $result1 = $_GET['num1']; $result2 = $_GET['num2']; $operator = $_GET['$operator']; switch ($operator){ case "None": echo "Please select entry"; Break; case "Add": echo $result1 + $result2; Break; case "Subtract": echo $result1 - $result2; Break; case "Multiply": echo $result1 * $result2; Break; case "Divide": echo $result1 / $result2; Break; } } ?>

What is wrong with above code? It does not works

Bilal Hameed says:

Hello, Can anyone please see what i am doing wrong in the following Code:

if (isset($_GET[‘submit’)){
$result1 = $_GET[‘num1’];
$result2 = $_GET[‘num2’];
$operator = $_GET[‘operator’];

As soon I write $_GET, after isset , I get an error “Do not Access Supperglobal $_GET Array Directly”. And on the the front side, when i do any calculation, i do not get any result. Can anyone see what i am doing wrong?

sohan singh says:

Very Very Very helpful tutorial, many my doubts are cleared now LOL thanks Man 😀

Farhan Awan says:

Your Way of Teaching is Wonderful Love You

Aravind Sanjeev says:

can i know the color scheme you are using on sublime editor?

BiT says:

This tutorials are pretty useful they’re not boring unlike many of another ones.

But if I punch in a text in the input fields, the browser throws an error. How to prevent this?

Can you help me with that?

JCL Trading Company says:

Its a great tutorial mmtus. Very well explained.
While I was building the calculator, I couldn’t get this worked in browser. I did, exactly as you did just copied yours.
But I was not able to figure out what went wrong as I am new to php learning.

< !DOCTYPE html>


Careerbuiz





The Answer is :

< ?php if (isset($_GET['submit'])) { $result1=$_GET['num1']; $result2=$_GET['num2']; $operator=$_GET['operator']; switch ($operator) { case "None": echo "You need to select a method"; break; case "Add": echo $result1 + $result2; break; case "Subtract": echo $result1 - $result2; break; case "Multiply": echo $result1 * $result2; break; case "Divide": echo $result1 / $result2; break; } } ?>

DJLukkie® says:

Help? what do i wrong?

< ?php if (isset($_GET['submit'])){ $result1 = $_GET['num1']; $result2 = $_GET['num2']; $operator = $_GET['operator']; switch ($operator) { case "none": echo "You need to select a method!"; break; case "add": echo $result1 + $result2; break; case "Subtract": echo $result1 - $result2; break; case "Multiply": echo $result1 * $result2; break; case "Divide": echo $result1 / $result2; break; default: echo "Error!!"; } } ?>

L Tulip says:

Very good tutorial for beginners.Thank you !Looking forward your new tutorials!

Write a comment

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend