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