Skip to playerSkip to main content
  • 11 years ago
Transcript
00:00Các bạn hãy đăng ký kênh để ủng hộ kênh của mình nhé.
00:30Các bạn hãy đăng ký kênh của mình nhé.
01:00Các bạn hãy đăng ký kênh để ủng hộ kênh của mình nhé.
01:01doing a test on each line.
01:03It's very similar if we've written if, else if, and else.
01:07The big difference here is that we're testing equality.
01:10We're testing to see, is the value equal to test value 1?
01:13No?
01:14OK, move down to the next one.
01:15Is value equal to test value 2?
01:17But it's a very simple comparison of equality.
01:20There's no greater than or less than or anything like that.
01:23And usually we're not working with booleans
01:25because a boolean is either true or false.
01:27So there's really not a lot of test cases.
01:29Where you would see it more is if you had something like a string
01:32that could be many different things.
01:35For example, let's say that our customers had told us
01:38that the way that they wanted to be contacted,
01:40and they might have said that they wanted to be contacted by email
01:42or by phone or by some other method, text message, Skype, something else.
01:47So we can have a switch statement that then executed a set of statements
01:50based on the way that they told us they wanted us to contact them.
01:54Now, I've only provided two case statements here and a default,
01:57but you can have as many cases as you need.
01:59You can just keep adding different cases each time.
02:02Now, most times, you're probably going to want if-else statements.
02:05You're going to use those way more often than switch statements.
02:07But switch statements are going to be very useful
02:09when you do have this kind of compact logic
02:11where you really are just saying,
02:13all right, I want to test a value,
02:15and that value can be 10 different things.
02:17So I'm going to list off the 10 different things that could happen
02:20if that value is each of those 10 cases,
02:22and I'm going to provide code for those.
02:24The parallel structure can be a little bit easier to read and to work with.
02:28Okay, now that we know the syntax and the basic idea,
02:30let's try some out.
02:31I'm going to create a new page where we can work with these.
02:33I'm going to open up basic.html,
02:35and I'll do save as,
02:37and I'm going to call this switch.php.
02:41Change the title.
02:42It'll also be switch,
02:44and let's give ourselves a PHP area.
02:48All right, so to start with,
02:49let's just give ourselves a variable.
02:51We'll call it a,
02:52and we're going to make a equal to 3.
02:54And then, let's do a switch statement.
02:57Switch, and then send the variable in as the argument to switch.
03:02So we're going to be testing variable a,
03:04and in the case where it is 0,
03:07then we're going to echo a equals 0.
03:11Put a br tag at the end,
03:13and the semicolon after that.
03:15Okay, now let's take that same thing.
03:16I'm going to paste it here,
03:19and let's make our next case that it's 1.
03:23All right, see what we're doing here?
03:24Let's see the parallel structure.
03:26I'm actually going to copy both of those now,
03:28save myself some typing,
03:30and we're going to make this one 2,
03:32and this one 3.
03:34I'll change it both here,
03:35and then the text.
03:36So there we go.
03:37Now we have our cases.
03:38Now remember, I could also have a default case.
03:40For now, I'm not going to do the default case.
03:42Let's just do the simple one.
03:44We're going to test whether it's 0, 1, 2, or 3.
03:47If it's not that, if it's 4, 5, or 6,
03:49well, then none of these will execute.
03:51It'll just move on right past this whole statement.
03:53So let's save it, and let's try it out.
03:56Let's go back to Firefox,
03:58and instead of logical,
03:59we're going to look for switch.php.
04:01A equals 3.
04:02Perfect.
04:03See how that worked?
04:04It skipped right past these cases.
04:06It tested them.
04:07It said, nope, nope, nope.
04:09Yes, this one matches,
04:11and so then it executed this statement.
04:13All right, let's just try moving A
04:15and making A equal to 0.
04:17Let's see what happens here.
04:18Save it.
04:19Let's go back to Firefox,
04:21and let's reload the page.
04:22Whoa, wait a minute.
04:24A equals 0.
04:25Well, that's correct.
04:26It did match that case,
04:27but look what happened after that.
04:28It also came back and told us
04:29that it was equal to 1, 2, and 3,
04:31which it's obviously not.
04:32And if we put another value in here,
04:34like 2,
04:35we go back and reload the page,
04:37you'll see that it comes back,
04:39and it executes the first one,
04:40and every case that comes after it.
04:42It's an important point
04:43about the way that switch statements work.
04:45They do the statement that matches,
04:47and then they continue to match
04:50every one that comes below it.
04:53Well, that's probably not what we wanted.
04:54So if you don't want that behavior,
04:57then you've got to provide
04:58a break statement.
05:00So break, break, and break
05:03after each one.
05:04Now when we go back,
05:05let's make it equal to 0 again.
05:08Save it.
05:10Go back to Firefox,
05:12and there it is, A equals 0.
05:13That's the behavior that we would expect
05:15because we told it,
05:16okay, you've done your statement.
05:17Now break out of the switch statement,
05:19and that will then drop down here
05:21to the curly brace
05:22and keep going from there.
05:23So this break is super, super important
05:25when you're working with switch statements.
05:27Drill it into your head,
05:28and you'll save yourselves
05:29a lot of debugging time.
05:31Now as I said,
05:31we can have a default case here as well.
05:33Default,
05:35and then echo,
05:37and then A is not
05:400, 1, 2, or 3.
05:44And we don't have to have a break here
05:45because it's the last one,
05:47but I'm going to go ahead
05:48just to keep the parallel structure
05:49and also just to keep reminding myself
05:51that that break is very important to have.
05:53So now I'll put it in there,
05:54and now let's just try A is equal to 9.
05:58Go back to Firefox,
06:00and there it is.
06:00It's not equal to 0, 1, 2, or 3.
06:02So you can see how that works.
06:03All right, I'm going to set that back to 2,
06:05and let's just drop down here,
06:06and I want to show you another example.
06:09This example is going to output
06:10the Chinese zodiac,
06:12the year that it is in the Chinese zodiac.
06:15So to do that,
06:16we're going to do a little bit of a calculation.
06:17We're going to take the year.
06:19We're going to use 2013,
06:21and I'm going to subtract 4 from it
06:23and do the modulo of that using 12.
06:26That means that I'm finding the remainder
06:28once we divide it by 12.
06:29So after we divide by 12 evenly,
06:31how many are left over?
06:33So that end result is going to be a single value.
06:37So even though I've got a calculation in here,
06:39it's a single value that comes through in the end,
06:41and that's what I'm testing.
06:42So whatever that value is,
06:44whatever that remainder is,
06:45it will check.
06:46If it's 0,
06:47then we are in the first year of the 12-year cycle.
06:50So it is the year of the rat.
06:52If it's a 1,
06:53well, then we know we're in the next year,
06:55and the year is the ox, and so on.
06:57What I want to show you here, though,
06:58is that white space doesn't matter.
06:59Do you see the difference here?
07:01Up here, we had each one of these on a separate line.
07:04Case 1, echo, break.
07:07Case 2, and so on.
07:08Here, I've got them all one after another.
07:11Very compact.
07:12Very easy to read.
07:13I've even added in some extra spaces here
07:15in front of the numbers
07:16to keep it all lined up.
07:18Nice, tight, easy to read.
07:19I've still got my colon.
07:20I've still got my semicolons
07:21to separate the commands,
07:22and I've still got break.
07:24But the white space doesn't matter.
07:25All right, just so we can see that,
07:26let's go ahead and bring that up in a browser.
07:28I'll put a BR tag here at the end.
07:31It pops up, and it says
07:332013 is the year of the snake.
07:35Now, there's one last example
07:36that I want to show you.
07:37I'm going to paste in another example here.
07:39Imagine that we have a user type.
07:40Let's go ahead and set a user type
07:42just so we don't get an error.
07:43User type.
07:45And the user type is going to be
07:46equal to customer.
07:48All right, it could be customer,
07:50it could be student,
07:51it could be press,
07:52it could be something else.
07:53Well, if that user type is student,
07:55then we're going to echo back welcome.
07:58If it's press,
07:59then we're going to say greetings.
08:01And if it's customer,
08:02then we're going to say hello to them.
08:04What I want to show you is
08:05what if you had multiple cases
08:07that you wanted to match on something?
08:09What if you wanted to have press and customer
08:11respond to the same set of statements?
08:13I mean, we could say hello
08:15and just do it there,
08:16but this is a simple example.
08:18I mean, imagine if this was, in fact,
08:2020 lines of code, right?
08:21So there's 20 lines of code.
08:22Am I going to really repeat
08:23that 20 lines of code again?
08:24No, I don't really want to do that.
08:26Now, this is the thing
08:27I want to caution you about.
08:29You may be tempted
08:30to do something like this
08:32and to say case, press, customer
08:34with commas between them.
08:35That's how you do it
08:36in some other languages.
08:37It's not how you do it in PHP, though.
08:40In PHP,
08:41if you want them to have the same one,
08:43well, guess what?
08:44You just put them one after another
08:45because what happens
08:46when press matches?
08:48It executes the statements
08:49that are here
08:49and then all the statements
08:51that are below it
08:52until it gets to a break.
08:54So press, customer
08:55is going to do
08:56the same set of statements
08:57because of the way
08:59the switch works.
08:59You can see now
09:00why the switch statement
09:01behaves the way it does
09:02and why it needs you
09:03to put in that break
09:04some of the time.
09:05It's because there are cases
09:06when you're not going
09:07to use the break
09:08but when you have
09:09multiple values like this.
09:10So we could have press,
09:11we could have customer,
09:12we could have case
09:14when it's admin, right?
09:16We can just add more cases
09:18and each one of those
09:18is going to do
09:19to this block
09:20that's below it.
09:21So let's save it
09:22and let's just try it.
09:23Let's go down here.
09:26We reload the page
09:26and there it is.
09:27Hello is what we get back
09:28as the result.
09:29That is the statement
09:29that goes with the case
09:31for customer.
09:31So as I said,
09:32you're going to use
09:33if statements
09:33a lot more
09:34than you're going
09:34to use switch statements.
09:35But if you look up here
09:36at something like this
09:37where you have
09:37the Chinese Zodiac,
09:39this is the classic case
09:40for when you're going
09:41to want to use
09:41a switch statement.
09:43Writing if else statements
09:44for all of this
09:44just doesn't give you
09:45the same legibility
09:46and ease of maintaining
09:48the code
09:48as the switch statement does.
Comments