I will call you back again to another video lecture in PHP.
And in this video lecture, let's call ahead and create a simple calculator with user input.
So the aim of this lecture is to be able to implement some calculations using the if statements.
We actually did using some inputs from a user.
So we go to create a form.
And then whatever the user puts inside that form we go ahead and either do an addition a subtraction
division or multiplication.
So we're going to see how that will be done.
So the very first thing we do is to go ahead and create a form and go ahead and close this form.
All right.
Now let's go ahead and uh set action.
And the PHP file is site dot PHP.
And then let's just go ahead and put the method and I'm going to use a post method.
Now that is posts.
So we go ahead and uh put in some numbers.
We're going to accept input from any user.
So go ahead say maybe first number.
And uh next one is second.
Now first none and second none.
All right.
So that is another one.
So in between them you're going to give the operator.
Now let's go ahead and say uh operator okay.
Let's say choose operator.
So it seems that is free to be.
So let's make this to be numbers.
So that is going to also be in a straight line.
So the user will enter the first number.
And then the reason we choose the operator he needs and then choose the second number.
Now the first number that the user inputs.
We are going to create a form for that.
So let's go ahead and say the input type is going to be number because that is what we need.
And then this guy had a name this to be number one.
Right.
So put out to be number one.
Go ahead and close this.
And there it can actually put a break right in here.
All right.
So let's go ahead and drag this a little bit.
And now for the update.
So we go ahead and create an input of text.
So the type is going to be text because I'm going to select either plus minus or division.
So let that be OP.
Let's say OPL are this operator.
Great.
So the next thing is.
So I'll go ahead and copy whatever I have right in here.
Go ahead and copy this and let's go ahead and paste that here.
So for this we'll also put a brick.
So it moves to the next line.
And the this is going to be num2.
Now let's go ahead and save this and refresh this page.
So we have first number set which is an operator.
Second number.
Then we're going to get the output.
Now we have created our form.
We need to go right inside PHP right now.
And then we're going to create some variables.
So we're going to create a variable for Num1 and num2.
So this is variable for Num1.
And then this is let's go ahead and create a second one.
So another variable for num2.
So for number one we're going to get the this we're going to get whatever the user puts in here.
So we go ahead and say no one is equal to um Darci on the score posts because I'm using the post method
and then we go ahead and pass in the name.
So this is the name.
Go ahead and pass in that right there.
So the same thing goes for the second one.
So underscore posts.
And then we pass in this the name.
And always remember to put a semicolon at the end of this is very important.
You always put that.
Now we're going to also create for the operator so that we always choose the operator.
So this is a placeholder to the OP.
Uh, and we go ahead and also use a post for that.
And then we go ahead and put OP uh, because that is the name that we have right in here.
So put semicolon at the end of this.
Now we're going to use if statement we just learned in the previous lectures.
And then go ahead and create It is a key statement and allow users to insert some numbers.
Right.
And then we'll go ahead and choose an operator.
So this decision statement is for the operator because we're going to determine we're going to check
if the user is set an addition subtraction division or multiplication signs.
Now for us to be able to do that I'll go ahead and see if if so the condition is if.
Now the variable operator.
So if the variable OPR with user insights is equal to plus sign, that means that is a division.
So we go ahead and are equal.
So go ahead and equal.
Num1 plus num2.
So put a semicolon at the end.
Now else.
What if it is not because we are looking for a but for conditions.
Addition, subtraction, multiplication and division.
So these are the four things we need.
Any other thing apart from this is an invalid operator.
So we're going to use an else if statement.
So we want to use an else if statement.
So we're going to check for the next operator.
So we're going to say op error.
So if op r is equivalent to let's say minus sign.
So we go ahead and are equal.
So go ahead and are equal.
Now one minus.
Not two for semicolon at the end of this.
Now we won't also check for the next condition because you have two conditions remaining the division
and the multiplication.
So let's go ahead and see if it is multiplied.
So go ahead and say else if else if.
The operator.
The opia operator is equivalent to let's say multiplication.
So it is equivalent to multiplication.
Then we go ahead and uh equal num1 multiply.
Num2 plus semicolon.
At the end of this now we have one more that is remaining.
So we go ahead and uh.
See.
Else if else if the last condition we have Is opia is equal to division.
So if is equal to division we go ahead and equal.
Number one divide.
Number two no one divide number two plus semicolon at the end of this.
So now we have made all the conditions.
So it's remaining and all statement because that is what we.
Are going to use to call the if statement.
And whatever you put outside this whatever you put.
Outside this is an invalid operator.
Now we go ahead and say.
Equal.
Invalid.
Operator.
So semicolon at the end of this.
And that is it for now.
Go ahead and save this.
And then let's go ahead and refresh this and check out.
So we have an error.
It says in line 22.
So line 22 says Unexpected Token LLC.
So now we open this.
We close this right in here.
So go ahead and close this.
So and now we save again.
Let's refresh and check.
And uh it says line one undefined array key number one on the final key number two and undefined Reiki
none.
Uh.
Line 16.
So this guy had an add in maybe two.
I would choose an operator plus, and we add in three.
So we did it.
Put in a submit button.
Now let's add a submit button right in here.
And with that we create the error that is in there.
And then we go ahead and puts Which type is equal to submit and go ahead and close that.
And let's save this.
So refreshing this.
So that is it has gone.
So that error is not a real an error from your code you understand.
So that is not an error from your code.
And that is why you can see we have no error.
Now we have invalid operator right in here because I have not selected anything.
So anything I select, anything you insert in here that is not within plus minus division and multiplication
is invalid.
Now let's go ahead and put in five and let's put plus and let's put seven is equal to 12.
You see that.
Now if we put two I will put division multiplication.
And we'll put in three.
And let's go ahead and push submit.
That is six.
So if you put in four put in division put in Two equal to two.
We see that.
So I hope that is cool.
So that is a way you can be able to actually do this.
So if I should go ahead and say seven and I'm going to say George and put in it, if I put in 7 or 8
and the submit is you can see a valid operator.
Now what if in the case you go ahead and say 6.7 and put in plus and put in three and go ahead and say
equal.
You can see we have an error that is set in here.
Please enter a valid value.
Then two nearest valid values are six and seven.
Because what we are creating this we did.
We only put into account the whole numbers the integer values.
Now if you want to add in decimals you go ahead and go right in here to numbers and put in step and
let it still be equal to 0.1.
All right.
Now, if I should go back here and save this and refresh.
This says submit.
Now I can say 6.7 and let's say plus okay.
If I say minus and let's say two is equal to 4.7.
Now what if I go ahead and say is 6.789 and go ahead and say this is a multiplication or division.
I don't know why I always press that.
So multiplication and not is five and then click on submit.
You can see please enter a valid value.
The two nearest valid values as 6.7 and 6.8.
Now you would be like what?
What is going on?
Now what you need to do is you go right in here.
You can say step is equal to 0.001 to accommodate more of a decimal places.
Go ahead and some of these are fresh.
Let's check it out.
Now I can get in at six.
6.789.
And go ahead and put in multiplication.
And then put in three is equal to 20.367.
So the same thing is applicable to number two.
If you want to add into put into consideration the decimal can go right in here.
Copy this and go right under here and say norm.
And then put in step of three decimal places.
So that is a way to add in uh the the you factor in that is the way you factor in the decimal places,
in your calculator, in your PHP program.
So I hope that is cool.
So pull it down.
Pull down with it.
The code is just very before you.
If you have any question, please use the question and answer section and I'm going to get back to you
as soon as possible.
Thank you so much.
And I'm going to see you in the next video lecture.