15: Exercise Using PHP | Let's Build a Calculator | PHP Tutorial | Learn PHP Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this episode we're going to talk about how to actually make something inside the browser using PHP code because in all the past episodes all we've talked about is all the fundamentals in PHP but we haven't actually done anything yet we can actually use inside a website so my thought was for this episode we're going to try to build a calculator inside the browser and yes I know a calculator is not the most interesting thing to build but it's a really good exercise for you guys to get into the mindset when you code PHP code because PHP is very different from HTML and CSS so if you guys follow along this episode and just kind of write code with me you guys should get the hang of PHP pretty quickly so as you guys can see right now I do actually have a regular HTML 5 set up inside a front page called index of PHP and this is document we're going to start working from now you guys should already know how to set up a document like this because you should know HTML CSS before you go into PHP so we're just going to go and get started now inside our body tag we're going to start setting up our calculator and my thought for the calculator was that we're going to have two inputs where we can actually write numbers in and then after the inputs we can decide with the drop down what kind of you know calculation we want to do if we want to add or subtract multiply or divide and then we're going to have a button where we calculate so we hit the button and then X it calculates it for us so the first thing we need to create in order to actually have a PHP calculator it's a form so I'm going to go ahead and open up a form inside by PHP inside my HTML code like so and the first I'm going to create this an input now the first input is just going to be a regular text type input so we're going to set the type as text and then afterwards we're going to go ahead and give it a name because when we use PHP code and we need to use it together that forms we need to use names inside the inputs to actually refer to those inputs when we call PHP code so in this case we're going to call the first one num 1 now we're also going to include a placeholder just so we have something we connect to see inside our browser so I'm going to set the placeholder as number one so after we save this I at least after we create this we're going to go ahead and copy it paste it below and then I'm just going to go and change the name to num2 and then i'm going to change the placeholder to number two so after we have this we can actually go ahead and refresh our browser just to see what we have so far and as you guys can see now we have two input fields so we can write something and this is good for now but we actually need some more stuff in here now the next thing we're going to include is a you know like a drop-down web connects you select if you want to add subtract multiply or divide so the way we're going to do that is by creating something called a select now a select is essentially just where we go in and give it a couple of options for the drop-down so inside the select tags we're going to go ahead and say option like so and then we're going to go and give the first one the name add at least the you know we're going to write add in between the option tags then we're going to copy the option tags just about maybe four more times five more times I think and then we're going to change the first option to none then we're going to change the third one to subtract the fourth one we're going to say multiply and the last one we're going to say divide so now that we have these we can actually go ahead and get the select one a name because like I said but you need to give it a name to refer to it when we use PHP code so I'm going to go ahead and give it a name inside the Select tag the name in this case we're just going to go ahead and set as operator like so now that we have this we can actually go and refresh the browser so you guys can see now we have a drop down we can actually you know select one of these options here so the next thing we need to do is include a button because we do actually need to be able to actually hit something and calculate so the underneath the select button we're going to give it a break because we need to go down to the next line and button and down here we're just going to go to add a regular button now inside the button tags we're going to go ahead and say calculate so we know what this button does and inside the button tag the tag itself we're going to go ahead and give it a name as well so we're going to set the name as submits afterwards we're going to give it a value as submit as well and then we're going to go ahead and set a type as well which is going to be equal to submit as well I'm saying as well a lot of times here but it really is a lot of repeating in this one so after we have this you guys will notice that we can now write something in here let's actually go ahead and write five in the first one 10 and the second one we can say add and calculate now what you guys will notice is as soon as we hit calculate up in the URL after our you know actual URL we're going to get a question mark and after the question mark we have num1 equal to five and number two equal to ten and operator equal to add and submit equal to submit so this is all the information we need in order to calculate and this is essentially how we pass on information with PHP because we usually use the UL to do this sort of thing so what we need to do now is actually go down and write PHP code that goes into the URL and fetches this information and does something with it based on what's up here so if we go down to our code underneath the form we're going to go ahead and start writing stuff so we need to open up the PHP tags like so now before the PHP tag we might want to add maybe some text that says you know here is the answer so we can actually you know let people know that the answer is going to be here so in between the form in the PHP tags I'm just going to add a paragraph and say this but maybe the answer is : and then we can actually start writing stuff inside the PHP tags now the first thing we need to do is actually check if we did actually hit the submit button because if we haven't hit the submit button yet and we're already checking for answers inside the URL it's going to give us an error message because there's nothing inside the URL if you have an execute anything yet as you guys can see up here so we need to first check if the calculate button has actually been hit so the way we're going to do that is by going into our PHP code we're going to go and say if so we're just going to go and write an if statement here then we're going to say inside the parentheses if is set which means that if this has been said already meaning that if we did actually hit the button parentheses again and inside the parentheses we're going to go ahead and check for submit so we need to go up into the URL and if there is in fact a submit that's equal to submit then it needs to do this kind of code down here so inside our parentheses we're going to say variable on the score gets because we're actually taking for a get method now we didn't actually talk about get and post method in PHP before we did talk about in the HTML CSS series but basically when we use a get method into the URL like pass an information into it we can actually see the information up here in the UL like we can right now if you use a post method we can't actually see the information in the URL but it's still there so in this case we can access seed information which means that it's a get method so we need to ask for get method then we're going to put brackets single quotes and in between here we need to ask what kind of information from the UL we're actually getting in this case it's the one that's called submit as a name so I'm going to go ahead and write submit so now we're actually asking if this is actually been set so we can actually go inside the if statement and start typing stuff so the first thing we're going to do is we actually going to go back into the URL and get the rest of the answers that we've typed in before or so we can actually go ahead and create a variable and let's just go ahead and call this one result one we're going to set equal to whatever is inside the URL which has what did we call num1 as a result so we're just going to go ahead and copy our get method up here paste it down here and of course change the answer from submit to num1 then we're going to copy this line paste it below a couple of times change the second line from result to a to result two and change the from num1 - num2 the last one down here we're going to call operator and then we're going to go and ask for operator because we did actually set our select up here to operator as a name so now we've done this we actually get the different answers from the URL itself so the next thing we need to do is actually to tell it what to do this information if we get a certain type now we can do this in two different ways either we can use an if statement or a switch statement in order to do something to results and since we did already use an if statement here I think we should actually try and use a switch so down here we're going to create a switch like so great parentheses curly brackets and then we need to tell it what we're actually trying to get inside the switch here so in this case we want to get the operator like so and put it inside our parentheses because we're actually checking for the operator you know if we did actually add we need to do one certain thing with the addition if we actually subtracted we need to do something else with the actual calculation so inside the switch we're going to set up a case I'm going to go ahead and say that if the case is equal to add then it should do something well actually the first one we have is actually the one called none so we should probably take that one that one first so if the user did not select anything it should do this so I'm going to go ahead and say if they did not select anything I'm going to echo out error because they do actually need to select something but we could actually just say you need to select a method like so so if you didn't choose anything you need to actually select the method and of course we do need to set the nun as a string so the next one all right we do actually need to add the break as well I forgot that like so so now we can just sort of copy this one paste it below a couple of times we need four more I do believe and then we're going to set the second one as add with a big a we're going to set the third one as subtracts we're going to set the fourth one I do believe it is as multiply let's actually get rid of that thing is letter will t apply there we go and the last one is going to be divided so now we need to actually calculate inside instead of echoing so in this case we're actually just going to go ahead and say echo and then we would like to take result one and say plus result two like so this is basically all we need down here we're actually going to go ahead and just copy the echo for the rest of them like so like so and like so so now we just need to change the plus symbol to a minus on the second one the third one we're going to change to a multiply and the last one is going to be divided so now depending on what kind of operator we choose inside our form it's going to choose one of these switches and the reason we need to set you know the numbers is so that when we do actually choose the switch or like a case down here it needs to do something with information that we typed in so now we can actually go to our browser if I just go ahead and go back so we don't have anything you guys conceded if I do actually go into number one and click five number two click 10 and then go into my drop-down and say I want to add them I can actually calculate and you can see we get 15 I can also go in and add 20 if I want to and 34 and maybe multiply and you guys can see we get 680 so this is how we can build a basic calculator using PHP code and this is going to give you guys a really good idea about how we use PHP code inside a website again calculators not very exciting to build but it's a really good exercise to show you guys how we use if statements and how you switches and that sort of thing when we build functions inside a website so hope you guys enjoyed this episode we would like to start talking about I think we'll take a couple more examples for next couple episodes just to show a couple of other ways we can use PHP code at least with the stuff we've learned so far we do actually need to learn a couple more things regarding you know fundamentals of PHP but in order to you know just to give you guys some examples so you guys don't get too bored I'm gonna go and spend a couple more episodes sharing gasps some other stuff so if you guys enjoyed and I'll see you guys next time
Info
Channel: Dani Krossing
Views: 257,570
Rating: 4.9198766 out of 5
Keywords: creating a calculator in php, build a calculator in php, php calculator, php calculator tutorial, build a calculator php, php build a calculator, exercises using php, php exercise, creating calculator in php, create a calculator php tutorial, calculator with php, calculator php, php, php advanced tutorial, PHP tutorial, mmtuts, php for beginners, how to create a calculator, how to make a php calculator
Id: d6dnCQS8DCk
Channel Id: undefined
Length: 14min 20sec (860 seconds)
Published: Sat Oct 31 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.