WEBVTT

00:00.290 --> 00:03.320
I will call you back again to another video lecture in PHP.

00:03.350 --> 00:10.460
And in this video lecture, let's call ahead and create a simple calculator with user input.

00:16.730 --> 00:24.950
So the aim of this lecture is to be able to implement some calculations using the if statements.

00:24.950 --> 00:29.390
We actually did using some inputs from a user.

00:29.390 --> 00:31.040
So we go to create a form.

00:31.040 --> 00:38.450
And then whatever the user puts inside that form we go ahead and either do an addition a subtraction

00:38.450 --> 00:40.280
division or multiplication.

00:40.280 --> 00:42.890
So we're going to see how that will be done.

00:42.890 --> 00:50.090
So the very first thing we do is to go ahead and create a form and go ahead and close this form.

00:51.560 --> 00:52.010
All right.

00:52.040 --> 00:55.670
Now let's go ahead and uh set action.

00:55.670 --> 01:00.020
And the PHP file is site dot PHP.

01:00.140 --> 01:05.710
And then let's just go ahead and put the method and I'm going to use a post method.

01:05.740 --> 01:07.990
Now that is posts.

01:08.350 --> 01:13.390
So we go ahead and uh put in some numbers.

01:13.390 --> 01:15.490
We're going to accept input from any user.

01:15.490 --> 01:18.130
So go ahead say maybe first number.

01:18.160 --> 01:22.360
And uh next one is second.

01:22.390 --> 01:27.820
Now first none and second none.

01:28.000 --> 01:28.510
All right.

01:28.540 --> 01:29.560
So that is another one.

01:29.560 --> 01:32.800
So in between them you're going to give the operator.

01:32.830 --> 01:39.340
Now let's go ahead and say uh operator okay.

01:39.340 --> 01:41.380
Let's say choose operator.

01:43.570 --> 01:46.210
So it seems that is free to be.

01:46.210 --> 01:48.250
So let's make this to be numbers.

01:48.250 --> 01:51.400
So that is going to also be in a straight line.

01:51.700 --> 01:55.270
So the user will enter the first number.

01:55.270 --> 02:00.280
And then the reason we choose the operator he needs and then choose the second number.

02:00.280 --> 02:03.370
Now the first number that the user inputs.

02:03.370 --> 02:06.040
We are going to create a form for that.

02:06.040 --> 02:14.050
So let's go ahead and say the input type is going to be number because that is what we need.

02:14.050 --> 02:20.500
And then this guy had a name this to be number one.

02:20.620 --> 02:20.920
Right.

02:20.920 --> 02:22.480
So put out to be number one.

02:22.510 --> 02:25.000
Go ahead and close this.

02:25.180 --> 02:28.870
And there it can actually put a break right in here.

02:31.720 --> 02:32.440
All right.

02:32.440 --> 02:35.110
So let's go ahead and drag this a little bit.

02:35.110 --> 02:37.840
And now for the update.

02:37.840 --> 02:42.520
So we go ahead and create an input of text.

02:42.520 --> 02:54.340
So the type is going to be text because I'm going to select either plus minus or division.

02:54.340 --> 02:56.170
So let that be OP.

02:57.220 --> 02:59.560
Let's say OPL are this operator.

03:00.310 --> 03:00.760
Great.

03:00.760 --> 03:03.030
So the next thing is.

03:03.030 --> 03:05.580
So I'll go ahead and copy whatever I have right in here.

03:06.090 --> 03:08.820
Go ahead and copy this and let's go ahead and paste that here.

03:08.850 --> 03:10.740
So for this we'll also put a brick.

03:10.740 --> 03:12.090
So it moves to the next line.

03:12.180 --> 03:16.320
And the this is going to be num2.

03:17.070 --> 03:20.550
Now let's go ahead and save this and refresh this page.

03:20.550 --> 03:24.930
So we have first number set which is an operator.

03:24.960 --> 03:25.980
Second number.

03:26.010 --> 03:28.260
Then we're going to get the output.

03:29.040 --> 03:31.140
Now we have created our form.

03:31.140 --> 03:33.990
We need to go right inside PHP right now.

03:33.990 --> 03:36.810
And then we're going to create some variables.

03:36.810 --> 03:40.290
So we're going to create a variable for Num1 and num2.

03:40.320 --> 03:42.300
So this is variable for Num1.

03:42.300 --> 03:47.040
And then this is let's go ahead and create a second one.

03:47.370 --> 03:50.280
So another variable for num2.

03:50.310 --> 03:57.120
So for number one we're going to get the this we're going to get whatever the user puts in here.

03:57.120 --> 04:07.770
So we go ahead and say no one is equal to um Darci on the score posts because I'm using the post method

04:07.980 --> 04:11.370
and then we go ahead and pass in the name.

04:11.370 --> 04:12.660
So this is the name.

04:12.690 --> 04:14.670
Go ahead and pass in that right there.

04:14.700 --> 04:18.180
So the same thing goes for the second one.

04:18.180 --> 04:21.780
So underscore posts.

04:21.780 --> 04:26.070
And then we pass in this the name.

04:26.100 --> 04:30.240
And always remember to put a semicolon at the end of this is very important.

04:30.270 --> 04:31.140
You always put that.

04:31.140 --> 04:37.980
Now we're going to also create for the operator so that we always choose the operator.

04:37.980 --> 04:41.250
So this is a placeholder to the OP.

04:41.250 --> 04:47.490
Uh, and we go ahead and also use a post for that.

04:47.490 --> 04:53.640
And then we go ahead and put OP uh, because that is the name that we have right in here.

04:53.640 --> 04:55.770
So put semicolon at the end of this.

04:55.770 --> 05:00.570
Now we're going to use if statement we just learned in the previous lectures.

05:00.570 --> 05:08.660
And then go ahead and create It is a key statement and allow users to insert some numbers.

05:08.780 --> 05:09.290
Right.

05:09.290 --> 05:13.190
And then we'll go ahead and choose an operator.

05:13.220 --> 05:19.130
So this decision statement is for the operator because we're going to determine we're going to check

05:19.130 --> 05:25.310
if the user is set an addition subtraction division or multiplication signs.

05:25.340 --> 05:33.560
Now for us to be able to do that I'll go ahead and see if if so the condition is if.

05:34.550 --> 05:37.760
Now the variable operator.

05:37.790 --> 05:45.920
So if the variable OPR with user insights is equal to plus sign, that means that is a division.

05:45.920 --> 05:49.880
So we go ahead and are equal.

05:50.300 --> 05:52.220
So go ahead and equal.

05:55.550 --> 06:02.090
Num1 plus num2.

06:02.120 --> 06:04.250
So put a semicolon at the end.

06:05.120 --> 06:06.560
Now else.

06:07.550 --> 06:13.310
What if it is not because we are looking for a but for conditions.

06:13.340 --> 06:17.720
Addition, subtraction, multiplication and division.

06:18.110 --> 06:19.850
So these are the four things we need.

06:19.850 --> 06:23.390
Any other thing apart from this is an invalid operator.

06:23.390 --> 06:26.600
So we're going to use an else if statement.

06:26.600 --> 06:29.690
So we want to use an else if statement.

06:29.690 --> 06:31.580
So we're going to check for the next operator.

06:31.580 --> 06:35.150
So we're going to say op error.

06:35.990 --> 06:40.370
So if op r is equivalent to let's say minus sign.

06:40.790 --> 06:44.480
So we go ahead and are equal.

06:47.120 --> 06:50.210
So go ahead and are equal.

06:52.310 --> 06:56.150
Now one minus.

06:58.400 --> 07:01.130
Not two for semicolon at the end of this.

07:01.160 --> 07:06.070
Now we won't also check for the next condition because you have two conditions remaining the division

07:06.070 --> 07:07.210
and the multiplication.

07:07.210 --> 07:10.030
So let's go ahead and see if it is multiplied.

07:10.060 --> 07:15.790
So go ahead and say else if else if.

07:17.830 --> 07:19.180
The operator.

07:21.670 --> 07:27.160
The opia operator is equivalent to let's say multiplication.

07:27.160 --> 07:29.470
So it is equivalent to multiplication.

07:29.470 --> 07:39.100
Then we go ahead and uh equal num1 multiply.

07:41.890 --> 07:44.260
Num2 plus semicolon.

07:44.260 --> 07:50.140
At the end of this now we have one more that is remaining.

07:50.170 --> 07:53.410
So we go ahead and uh.

07:53.680 --> 07:54.760
See.

07:58.600 --> 08:09.750
Else if else if the last condition we have Is opia is equal to division.

08:09.750 --> 08:15.060
So if is equal to division we go ahead and equal.

08:17.520 --> 08:21.480
Number one divide.

08:24.150 --> 08:31.830
Number two no one divide number two plus semicolon at the end of this.

08:32.010 --> 08:34.650
So now we have made all the conditions.

08:34.650 --> 08:37.800
So it's remaining and all statement because that is what we.

08:37.830 --> 08:39.810
Are going to use to call the if statement.

08:39.810 --> 08:43.770
And whatever you put outside this whatever you put.

08:43.800 --> 08:46.860
Outside this is an invalid operator.

08:46.890 --> 08:48.780
Now we go ahead and say.

08:51.060 --> 08:51.840
Equal.

08:55.950 --> 08:56.850
Invalid.

08:59.940 --> 09:00.750
Operator.

09:01.320 --> 09:03.900
So semicolon at the end of this.

09:04.110 --> 09:05.640
And that is it for now.

09:05.670 --> 09:07.350
Go ahead and save this.

09:07.620 --> 09:10.890
And then let's go ahead and refresh this and check out.

09:10.890 --> 09:12.630
So we have an error.

09:12.660 --> 09:14.880
It says in line 22.

09:14.910 --> 09:22.470
So line 22 says Unexpected Token LLC.

09:23.130 --> 09:25.500
So now we open this.

09:25.530 --> 09:26.940
We close this right in here.

09:26.940 --> 09:28.320
So go ahead and close this.

09:30.420 --> 09:32.760
So and now we save again.

09:32.760 --> 09:34.380
Let's refresh and check.

09:34.380 --> 09:45.420
And uh it says line one undefined array key number one on the final key number two and undefined Reiki

09:45.450 --> 09:46.260
none.

09:46.650 --> 09:46.860
Uh.

09:46.860 --> 09:48.030
Line 16.

09:48.630 --> 09:51.600
So this guy had an add in maybe two.

09:51.630 --> 09:56.010
I would choose an operator plus, and we add in three.

09:56.040 --> 09:56.850
So we did it.

09:56.850 --> 09:58.680
Put in a submit button.

09:58.710 --> 10:00.930
Now let's add a submit button right in here.

10:00.930 --> 10:03.240
And with that we create the error that is in there.

10:03.240 --> 10:12.650
And then we go ahead and puts Which type is equal to submit and go ahead and close that.

10:12.650 --> 10:13.910
And let's save this.

10:13.910 --> 10:15.380
So refreshing this.

10:15.380 --> 10:17.420
So that is it has gone.

10:17.420 --> 10:22.790
So that error is not a real an error from your code you understand.

10:22.790 --> 10:24.560
So that is not an error from your code.

10:24.560 --> 10:26.540
And that is why you can see we have no error.

10:26.570 --> 10:31.970
Now we have invalid operator right in here because I have not selected anything.

10:31.970 --> 10:39.200
So anything I select, anything you insert in here that is not within plus minus division and multiplication

10:39.200 --> 10:40.070
is invalid.

10:40.100 --> 10:47.780
Now let's go ahead and put in five and let's put plus and let's put seven is equal to 12.

10:47.810 --> 10:48.740
You see that.

10:48.770 --> 10:53.720
Now if we put two I will put division multiplication.

10:53.720 --> 10:55.280
And we'll put in three.

10:55.280 --> 10:57.350
And let's go ahead and push submit.

10:57.350 --> 10:58.580
That is six.

10:58.580 --> 11:08.840
So if you put in four put in division put in Two equal to two.

11:09.200 --> 11:10.100
We see that.

11:11.150 --> 11:12.500
So I hope that is cool.

11:12.500 --> 11:16.580
So that is a way you can be able to actually do this.

11:17.210 --> 11:27.470
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

11:27.470 --> 11:30.410
and the submit is you can see a valid operator.

11:30.440 --> 11:41.240
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

11:41.270 --> 11:41.930
equal.

11:41.990 --> 11:44.330
You can see we have an error that is set in here.

11:44.360 --> 11:46.970
Please enter a valid value.

11:47.000 --> 11:51.410
Then two nearest valid values are six and seven.

11:51.410 --> 11:54.170
Because what we are creating this we did.

11:54.290 --> 11:59.030
We only put into account the whole numbers the integer values.

11:59.060 --> 12:07.030
Now if you want to add in decimals you go ahead and go right in here to numbers and put in step and

12:07.360 --> 12:10.450
let it still be equal to 0.1.

12:10.720 --> 12:11.110
All right.

12:11.140 --> 12:14.470
Now, if I should go back here and save this and refresh.

12:14.470 --> 12:16.120
This says submit.

12:16.150 --> 12:21.160
Now I can say 6.7 and let's say plus okay.

12:21.190 --> 12:27.310
If I say minus and let's say two is equal to 4.7.

12:27.310 --> 12:37.420
Now what if I go ahead and say is 6.789 and go ahead and say this is a multiplication or division.

12:37.420 --> 12:39.490
I don't know why I always press that.

12:39.490 --> 12:46.180
So multiplication and not is five and then click on submit.

12:46.300 --> 12:48.850
You can see please enter a valid value.

12:48.880 --> 12:53.890
The two nearest valid values as 6.7 and 6.8.

12:54.370 --> 12:56.290
Now you would be like what?

12:56.320 --> 12:57.160
What is going on?

12:57.190 --> 12:59.440
Now what you need to do is you go right in here.

12:59.440 --> 13:05.590
You can say step is equal to 0.001 to accommodate more of a decimal places.

13:05.620 --> 13:08.320
Go ahead and some of these are fresh.

13:08.350 --> 13:09.340
Let's check it out.

13:10.000 --> 13:11.380
Now I can get in at six.

13:11.410 --> 13:13.720
6.789.

13:13.720 --> 13:17.410
And go ahead and put in multiplication.

13:17.410 --> 13:24.040
And then put in three is equal to 20.367.

13:24.040 --> 13:26.740
So the same thing is applicable to number two.

13:26.770 --> 13:32.320
If you want to add into put into consideration the decimal can go right in here.

13:32.350 --> 13:35.560
Copy this and go right under here and say norm.

13:35.560 --> 13:39.010
And then put in step of three decimal places.

13:39.010 --> 13:48.100
So that is a way to add in uh the the you factor in that is the way you factor in the decimal places,

13:48.100 --> 13:52.960
in your calculator, in your PHP program.

13:52.960 --> 13:54.190
So I hope that is cool.

13:54.190 --> 13:55.270
So pull it down.

13:55.270 --> 13:56.200
Pull down with it.

13:56.230 --> 13:57.760
The code is just very before you.

13:57.790 --> 14:02.950
If you have any question, please use the question and answer section and I'm going to get back to you

14:02.950 --> 14:04.060
as soon as possible.

14:04.090 --> 14:05.110
Thank you so much.

14:05.110 --> 14:08.080
And I'm going to see you in the next video lecture.
