0:00
Guys, in today's video we will see the switch statement
0:03
that how you can use the switch case statement and how switch case can save your time in PHP
0:13
Guys, if you haven't accessed the PHP playlist yet, then kindly access it
0:17
This is the playlist where I am uploading all the videos time to time
0:22
So if you haven't accessed this playlist yet, then access it. Now we will move forward to the switch case statement
0:29
So this was our last video which was video number 10. Now I am making a video number 11 here
0:34
And I am going to keep it as switch case switchcase.php And here what I will do is php
0:46
and along with that I will end it by marking it as question mark
0:50
Now why do we need this switch case statement? And is it similar to the if-else statement or not
0:55
So the answer to the question is yes, it is very similar to the if-else condition
1:00
But the way it works is completely different. For example, let's say I make a variable here as $age is equal to 67
1:08
And now what I will do is I will write switch here. And after this I will write age here
1:14
Okay, so I will write $age here. And what I will do here is
1:18
according to the values of this variable, I will want to perform some operations
1:24
For example, case 12, I will want to print here, I will write echo
1:30
and I will write you are 12 years old. Then after that what I will do is I will write another case
1:39
45 and after that I will write here you are 45 years old
1:47
Okay, after this I will write here case 56 and I will write echo
1:53
you are 56 years old boy. Keep typing semicolon and I can write as many cases as I want
2:03
I can execute whatever code I want. Okay, and along with that
2:09
I will put a default here at the end and I can write echo here
2:13
and after that I will write your age is weird. Okay, now I am writing anything here
2:20
Don't think that what I am writing has any meaning. It is my job to explain you the flow of the program
2:27
Okay, so what I did here is 12, 45, 56 and default
2:32
Put these four cases. My age is written above 67. I did control s
2:39
and what I will do here I will go to this and open the file with the switch case
2:43
and it says that there is an error in line number 19 when I may have made some error
2:48
Why is it saying that there is an error? Yes, it is telling me that there is an error in dollar age line number 19
2:54
Yes, so here I have made a mistake. This switch I will have to open it by putting curly braces like this
3:01
I made a small mistake which you may also do and these mistakes are made by those people
3:05
who have done programming in a language like Python because everything runs according to indentation there
3:10
So, you have to put your switch in this curly braces. So, this is the correct syntax
3:16
Okay, so now I will reload. See, it is saying your age is weird
3:19
because it is neither 12 nor 45 nor 56. Now, let's say I make it 12
3:25
Let me show you 56 first to keep it simple. Okay, I will do 56
3:30
I will reload your you are 56 years old boy. Your age is weird
3:34
You must be thinking that 56 years old boy is understandable. Why is your age is weird printed
3:42
This case is matched but why is the following case printed? I don't know why it is printed
3:48
You must be thinking that but I will tell you why it is printed. But before that
3:52
I will put a br here. And this age is equal to 56 is written
4:01
I will make it 12. That means I am matching the first case
4:07
And now if I reload, see all the cases are printed. You must be thinking that what happened
4:12
So, what happens is in switch case statements if any one case matches
4:16
then it executes all the cases after that. Like if I show you
4:20
or if I match 45 then see 45 matches 56 comes your age is weird also comes
4:27
that means from 45 to default all the cases were printed. Now you will say that
4:32
I want that only my case which is matching should be printed
4:37
Why is it printing all of them? So this thing sometimes gets frustrating
4:41
So the solution is simply what you do is press ALT I am telling you this only for VS Code
4:47
this is for editor only. The solution is here after every case
4:54
put a break statement. What will happen with this? Save it