 Hey everybody, this is Brian and welcome to the 13th lamp tutorial. Today we're going to be covering the PHP switch statement. Our last tutorial we covered if, else, and else if. Well, what happens if you've got a massive amount of if statements? Well, it becomes very cumbersome and cluttered. The switch statement is identical in function, but has a more elegant syntax. It looks a little confusing when you first look at it, but once you break it down it's actually fairly simple. You say switch, condition, and then different cases. Notice how it says case, label, notice the colon, and then code, and then notice this break command. What does this do? Switch, that's your if statement. So you're saying switch on this condition, the condition being whatever you want, and we're saying case, label, case, label. What is a label? A label is a value, so we're saying switch on n. Case, label one, label two, default. Default is if you fall all, it's called falling through. If you fall all the way to here, default means this code will always be executed. Notice the breaks. If you switch and the label is identical, it will execute this code. The break tells it to stop executing. It says use break to prevent the code from running to the next case automatically. The default statement is used if no match is found. Pretty simple. So here is an actual example. X equals one. Switch, X, case one. So you guessed it, this is going to execute echo number one. So what we're going to do here is we're just going to type this out very neatly, very easily. And let's just say, I'm trying to think of a better example here, name, all right. Name equals, let's use your name. My name's Brian. You can just enter your name. Switch. So we're going to switch on our name. And then we're going to say case. Some languages cannot switch on a string. PHP however can. Now what I always do, I always make one of these as a template, so to speak. Oh, sorry about that. I had to dump my laundry off in the dryer. Okay, let's see here. Let's not do your names, but let's say you are not me. So I'm going to say case Bob. And then what I do is I just copy this whole thing and then I'll paste it out a few times. I'm going to kind of manually override the indentations there just so you can see. You are not even a guy. Case Brian. That's pretty bad when you can't spell your own name there. You are me. Actually, let's just say it's me. Now when we save and run this, you'll notice how it says our variable is name. My name is Brian. We're assigning that. Notice the single equals sign. We're calling out a switch and we're saying switch on the name variable. So the switches, think of these as light switches. On, off, on, off. Okay? So every case is a light switch. If Bob equals Brian, the switch is on, but it's not. So we jump right over this code. Case Sally, sorry. Think of that as a light switch. Is that switch on or off? Well, it's off because Sally does not equal Brian. Case Brian. This light switch is an on or off. You guessed it. It's on. So it prints out that code. Default, who are you? We didn't get the default because of this break. What happens if we take that break out? See how it says it's me? Who are you? That's why you need a break. That's called follow through. It executed this code, but because there was no break telling it to break out of that code block, it just falls down to the next, next one. Now the break command is something that we'll use in other, other situations. For example, loops break tells the current context or the executing context to break out of that. In short, you have this block of code. You're executing it. When you encounter a break statement, it tells it to break out of that code. So it lands here. Pretty easy. Now let's change this. Let's say my old cat's name was Othello. He died, unfortunately. I know. That's pretty sad. Now let's save that. Notice how it says who are you? That's because, think of these as light switches again. Is this light switch on or off? Does Othello equal Bob? No. Is this light switch on or off? Does Othello equal Sally? No. And this light switch on or off? Does Othello equal Brian? No. Because we haven't equaled any of these, it executes the default case, which we can actually just indent that so it's a little more clear what happens. Remember, default's the catch-all. This will always be executed if you haven't already broken out. Pretty simple. Pretty easy to understand. That's all for this tutorial. We're whipping through these fairly quickly. If you have any problems, I would suggest you go out and go to w3schools.com or you can actually go to numerous user forums out there. Usually the PHP community is extremely helpful. PHP is one of the most popular programming languages out there. Just experiment. Play around. That's how you learn.