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