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