Switch Statements

Hey guys welcome back in this video we will be looking at switch statements with PHP. In our previous endeavor we would have looked at our first selection statements which would be an if else our if, else if else statement. In this video, we will be looking at switch statements which kind of do the same thing allowing us to make a decision based on a value, but it’s just a different way of accomplishing it. Now on screen I have my Index page which has been our landing page for this PHP primer website. And the next page that we will create is one so we can have our switch statement examples on so I’m just going to go ahead using Visual Studio Code creates a new file in the same folder, and I’m going to call it switch statement dot php. Next up, we want to generate our html5 templates. So we do that by just typing in HTML colon five in Visual Studio code and then we Get our templates, I will change this to switch statements for the page title. And before that I would put PHP, primer and a hyphen. Now, it’s always a good idea to let your users know what page they’re on. So I’m just going to add an h1 tag to the top of this page to see which page it is that you’ve landed on. And just see switch statements. And I just copy this code and use it also in the if statement page because we didn’t do that. And I will just paste that there and see if statements if or else statements Alright, now that we’ve gotten those semantics and aesthetics sort of the way let’s get down into our PHP so we invoke our PHP tags and once again, always open and close at the same time, and then we start getting our missy. So I’m going to kind of routine the example from the if statement video and we will just declare a variable called grade, and we will give it some letter grade value, and we will see a, alright, no, a switch statement pretty much has the word switch, and then we will put the variable that is under scrutiny. So what happens is that we see a switch, and then we pass in some variable, which implicitly has a value as we saw here, we just declared greed and give it a value of a and then we will define cases and then make a decision based on the case. So that’s the big picture. Let’s get into it. So we see a switch variable name which is greed. Alright, and then we open up our curly braces, and then we see keys. And then the case would pretty much be seeing is the value equal to this or not? Not so the case that I want is that when it is a, I want to make a take a certain action. So I say case, then value that I want to compare the variable here in the switch statement to, then I say colon, and then in the next nine I write as much code as I need to to get the tasks done. In this situation, we just want to echo a statement, say if the case is a, if the grade is a, then I’m going to echo You are a superstar. So, we’re going to use the same statements from the if video, we just say you are a superstar, we’re putting that inside of h two tags. Alright, so if that is the case, if the case is that grid is is the value a right I guess you can read it like that. Then we’re going to echo You are a superstar in each two tags with a semicolon at the end of the line. And then we have one more key word here that’s as necessary, which is break. So if we have multiple cases which are about to have, if we don’t have the break keyword, then it will actually run through that case and every other case until it hits a break. So you always want to include your break. After you finish defining your options, just put a break. All right, and the indentation actually helps us to see where everything starts. So we say switch, then we indent, and we have all the cases on one in the indentation and then we have all the actions per case on another indentation that just leads to better readability. So the next case that I would have would be case and value is B. And if the value is B hat, I’m just going to jump over to my if statement code and borrow what we did here. And you see that we had used the CSS stuff there. Okay, let me use this one. Sorry. Then you want to echo You did well, so I’m just going to echo You did well, when the case is BNC. The in addition didn’t quite work out. So I’m going to deliberately indented because I like readability. And then after that I’m going to break. And I’m just going to go ahead and include the CSS in this one, I believe you are superstar should print in green. All right. So we’re just using the same stuff from the if statement code, where we printed in an h2 tag with the color green that you are a superstar if it’s an A, and if it is, a B gets in blue, etc. So I’m just showing you that the if statements, and the switch statements have a lot in common, all right, and then next, we want to see default. So you can define as many cases as you need to, and each cases would be akin to like an if else if and the condition. But then if you run out of all the conditions and you’re looking for a value that might be no life Then you would say default. So default would be the switch statements equivalent to the else in an if statements. All right, so we say a default, and then the default would print, you have failed. So I’m just going to borrow this. And I’m just going to paste it down here, make sure I indented and I’m going to bring that into red. And then the text should see you have failed dot, dot, dot. All right, I then once again, we have our break statement. So if you had multiple things to do, maybe you did a little math, you did a little process and then you wanted to print it doesn’t matter how many lines of code come within a case just make sure that it comes after the colon and before your break. Alright, so if I had multiple things to do here, I wanted to print this and print something else and print and print and do this and print. Then I just have all of those lines. You In the colon, and the brique, per case, and then all the cases are defined inside of your switch statement, which is the word switch, you open your regular brackets or a parenthesis, put in the variable that needs to be compared with something, and then open and close your brace your curly braces, and then you define your code in between. Now, let’s test the theory. So if I bring up my browser, and I go back to my home page, and a good rule of thumb with web design is that from any page, you should be able to get to every other page. so in this situation, we’re not going that in depth, we’re just looking at PHP and then when we start building our applications, then we will see it properly. But for now, we have all the links set up on our index page. And the next link up would be our switch statements. When I click that we see the text you are a superstar why because Or grade has the value a. And then when it’s it said when, you know make a decision on the value of a, and if the case is that the value sorry, make a decision on the value of greed. And then if the case is that the value is a, then t that action so if I change this to be, then they expected results, sorry about that expected results is that we should get you did well in blue. And if I change it to see, we get red text, citing your failure, all right, and I left outer space and we just add that there and then if it’s any other value, meaning if I put a four g, then well you have failed at the end of the day because default is the catch all was as long as these cases are not met meaning the case is not that the values a The case is not that the values be then default is our catch all that will just take certain actions accordingly. That’s it for switch statements. They’re relatively simple and as you go along you’ll see the relevant needs for them.