Building a Better Calculator | PHP | Tutorial 22

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I'm gonna show you guys how to build a four function calculator in PHP now if you've been following along with this course you'll know that in the beginning of the course we actually created a very basic calculator the calculator basically just allowed the user to enter in two numbers we took those numbers and then we added them together and printed the answer out onto the screen in this tutorial though I'm going to show you guys how we can build a fully functional calculator which can do not only addition but subtraction multiplication and division and we're gonna allow the user to decide which operation they want to perform so this is gonna be pretty cool and we'll get to use if statements in order to do this so down here inside of this program I have this form that I set up in my HTML and the action is just cite PHP that's this file and then the method is post and then I have this submit button so the first thing that I'm gonna want to do if I'm gonna build this calculator is I'm gonna want to be able to get information from the user and actually for this particular calculator we're looking for three pieces of information we're looking for the first number we're looking for the second number and we're also looking for the operation that they want to perform so not only don't want to know what two numbers they want to use but I also want to know if they want to add subtract multiply or divide so we're gonna create input boxes for all of those different things so I'm just gonna say input type is gonna be equal to number so we're gonna use this to get the first number and then I'm gonna give this a name and I'm just gonna set it equal to num one and then I'm just gonna put a break over here so this is gonna allow us to get num1 and i'll basically just say first num and then what I'm basically just gonna do is copy this and we can use this same template to get the second number so over here I'll just make a new line and we'll paste this down here for the second number so now we're able to get the first number and we're able to get the second number I'm going to change this to num2 and the last thing we want to do is we want to get the operator so I'm just gonna say Opie and that's gonna stand for operator and I'm actually gonna create another input I'm just gonna say type and we're going to set this one equal to text box so we're gonna allow the user to type in like a plus sign a minus sign a multiplication sign or a division sign and then over here I'm just gonna say name and we're just gonna say this is gonna be Opie once again for operator and then we'll put a break over here so I have three input boxes the first num second num and the operator these two are getting a number and this one over here is just getting text and actually this should just be text not text box my bad so now we're able to get information from the user and if you see over here on my web page that should all work out so we have you know boxes for each of these different inputs and then we have our submit button so our job now is to get that information I'm actually going to store it inside of different variables and then we need to figure out what operation they wanted us to perform so down here let's just create a few different variables so I'm gonna create a variable we'll just call it number one and I'm gonna set this equal to whatever the user entered so it's gonna be post and we're gonna get the number from the num1 number box and then I'm gonna do the same thing for num2 so I'll say num2 is gonna be equal to post and num2 and then finally i'm gonna do the same thing for the operator so I'm just gonna say oh P which is gonna stand for operator is gonna be equal to post and here we're just gonna get opie so these two are gonna be numbers and this one is gonna be a string of text so what we want to do now is we want to figure out what operation the user wanted to perform in other words we have these two numbers right and it doesn't really matter what those are but we also have this operator and in order to figure out if we need to add the numbers subtract them multiply them etc we need to figure out what's inside of there in other words we need to figure out if it's a plus sign or if it's a minus sign and in order to do that we can use an if statement so by using an if statement will allow our program to respond to this value so down here I can just say if may can open and close parentheses open closed curly bracket and the first thing I want to do is check to see if this is a plus sign so I can basically just say if Opie is equal to a plus sign and if the operator is equal to a plus sign then we can basically just echo out num1 plus num2 so if it's a plus sign then we'll just write out the answer so it's gonna be num1 plus num2 and then we can keep checking different things so if that's not the case then I can say else if and I'll come down here and I'll check to see if the operator is equal to a minus sign if the operator is equal to a minus sign then we can just echo out num1 - num2 because if this condition over here is true that means we want to subtract the numbers and I can actually do the same thing for division and multiplication I'm gonna copy this and I'm gonna paste it down here because we're basically doing the same thing and I'm just gonna say division sign so I'm gonna do that forward slash and I'm gonna make a forward slash here and then I'll paste this one more time for multiplication so here it's gonna be an asterisk and then down here we will multiply them so this if statement is basically checking if it's a plus sign if it's not it's checking if it's a minus sign then it's checking if it's a division sign or a multiplication sign but there's also one more situation that could occur and that's when the user entered in an invalid operator so they didn't enter in one of these four up here so we can just say else and then down here I can just echo out an error message so I could just echo out like invalid operator right so that'll cover the case where the user entered in an invalid operator so basically this if statement is going to allow me to figure out what's inside of the operator variable in other words it'll allow me to figure out what the user entered as an operator and depending on what they entered I can perform that operation down here so this should be fully functional why don't we refresh the page and we'll go ahead and run this program so you can see down here it's saying invalid operator that's basically just because I haven't entered anything yet so if I come up here I can say first number I don't we say this is gonna be ten and why don't we do addition and then the second number is going to be like 35 so now when I click Submit it should add those numbers together so it's gonna look through that if statement figure out which operator we submitted and it's going to do the operation for us when I click Submit you see we get 35 so why don't we do 30 and we'll multiply 30 by 2 so now we should get 60 and you can see down here that we do so that's actually working pretty well let's do one more so I'm gonna say like 45 and let's make the operator like some nonsense like draft and then we'll just say 35 35 so this is actually gonna be an invalid operator so our program will recognize that and it will show us so down here we were able to use this if statement in order to figure out what the operator was that the user entered and that is actually pretty awesome so hopefully you can see how that works and you can kind of see like you know how something like this could be useful and I also want to show you guys one more thing and this isn't like directly related to this calculator but it's a little thing that I think some people might be confused about so actually if I came over here into my calculator program and I entered in like a decimal number so if I entered in something like 4.6 and I click Submit you'll see that we're actually getting this error here and it says please enter a valid value and actually let me do it again so we get that the nearest values are four and five so it didn't actually let me put in here a decimal number and that's actually has to do with how HTML works and it basically just has to do with how this input tag works over here so by default when we say number this is only gonna take like whole numbers but we can actually modify that so we could make it so we could use decimal numbers so I could just say over here step and I could set this equal to like 0.1 for example and now this is gonna specify that we can take numbers to this decimal point so we can basically say numbers to the tens place so over here now I should be able to enter in a four point six with no problems so I could say like four point six plus 5.0 and it'll be able to do that math for us but you'll notice if I tried to do like four point five six seven this is gonna throw an error again because the step is not that significant so if I said step is 0.001 now I'm gonna be able to enter in a number just like this so I could say like four point five six seven plus and I could do like nine and now it'll be able to add these numbers together so that's not necessarily like a PHP limitation that's more of an HTML limitation but if you were a little bit confused about that hopefully that clears it up a little bit but you know the main point of this tutorial was to kind of show you guys how we could use an if statement to figure out what a user inputted into our program hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop Academy to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying traffic Academy and you want to help us grow head over to draft Adam EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 4,251
Rating: undefined out of 5
Keywords: Programming
Id: gTh-Vm32YgU
Channel Id: undefined
Length: 10min 0sec (600 seconds)
Published: Wed Nov 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.