PHP-and-MySQL/C2/Switch-Statement/English
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:00 | Hello and welcome to this PHP tutorial on the SWITCH statement. |
0:10 | I'm going to show you a new exercise on this because its an important feature of PHP |
O:16 | Let's create the syntax quickly |
0:20 | The SWITCH statement is a substitute for the IF statement. Its a lot more neater and formattable choice although the input is an expression. |
0:38 | So, now let's input the value of something and then let's save the value equal to this |
0:45 | Then we can execute the code if it equals or matches this value |
0:53 | It's not a comparison technique. So, for comparing an IF statement for matching values and outputs that depend on the input, we're going to say SWITCH |
1:03 | Let's start |
1:05 | SWITCH is the basic code for it |
1:10 | Let's put an expression in here, for example, I will say Alex here |
1:17 | Let's create a mini program and I will explain it as we go |
1:20 | Just like the IFstatement we will put curly brackets here |
1:26 | Now let's look at the way to call each sort of check |
1:30 | We want to check the value here |
1:33 | Now we will put this in quotation marks |
1:37 | You can't even number obviously |
1:42 | So what we type is - case - the value of the case that we want to match. For example- Alex |
1:54 | Then we type a colon or a semi colon |
1:58 | And then the condition if the case has matched with the SWITCH expression you have picked |
2:05 | So, I will type - echo you have blue eyes |
2:13 | To end our case comparison we're going to use break and a semi colon |
2:20 | Remember that we've used the semi colon here but not here |
2:25 | Now the second case. Lets see how to do it |
2:30 | I'll type Billy and echo you have brown eyes |
2:42 | Okay, and then break and semi-colon |
2:47 | This is like an integrated IF. That is I could say - IF your name is Alex then echo you have blue eyes or ELSE IF your name is Billy you have brown eyes |
3:00 | Probably for some people it's easy to do it this way. It's a lot more readable but it's a matter of choice |
3:11 | Okay we got no more cases, I'm just going to use Alex and Billy for this example |
3:20 | Here I will say default which will echo out - I don't know what color your eyes are |
3:29 | Okay, we don't need a break after this because there are no more cases |
3:38 | Obviously, there's no break after it cause there are no more options to chose from |
3:43 | Okay, so, we've got our SWITCH here. Let's give it a go |
3:48 | Now I'm going to replace this ALEX here with a variable, to build our program |
4:00 | So I type name equals, and I will let you decide that |
4:05 | Then I'll say name, here |
4:07 | So you see this is how we incorporate a variable here |
4:11 | You should know how to do that by now |
4:13 | So, let's start and see how this will work |
4:18 | You'll say switch, you'll take this expression, which is equal to Alex |
4:24 | Basically, this is the case which equals to Alex and it'll echo this. The break is to end it |
4:32 | And this is the case, Billy and it'll echo this and end with a break |
4:39 | If the name is say, Rahul, the default will echo - I don't know what colour your eyes are |
4:47 | Okay, so let's try running this |
4:49 | Just to revise |
4:52 | We can see that Alex matches to Alex matches to the output |
4:57 | What you can do is you can enter as many lines of code here as you like. This break determines where the case ends |
5:05 | An IF statement needs curly brackets to end a block |
5:12 | However, here break determines the end of the block. These are called blocks, by the way. |
5:17 | So, let's change this to Billy and let's see what happens |
5:22 | You have brown eyes - exactly what we determined here |
5:28 | Okay, now I'll change this to Kyle.....and refresh, I don't know what colour your eyes are, because there is no block that states Kyle's eye colour in our program feature |
5:45 | So, that's basically the SWITCH statement |
5:47 | Try it out. Some people don't like using it, some prefer using it |
5:50 | It's probably much faster than the IF statement. It's easier to control. It looks a lot better. So it's really up to your personal choice |
5:56 | Thanks for watching. This is Arvind for the Spoken Tutorial Project, signing off. Goodbye. |