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