From the course: PHP for WordPress
Switch and match statements
From the course: PHP for WordPress
Switch and match statements
- [Instructor] Switch and match statements are other control structures that help us make decisions. You may come across them in WordPress code, so let's learn about them. And we'll look at switch statements first. So you can see here that we have a variable called total that is 10, and our switch statement starts with the keyword switch. In parentheses, we put the variable that we want to evaluate and then we have the keyword case and a number. We can have any number of cases here, but each one is going to match a variable. So we have the keyword switch and then the parentheses with the variable we want to evaluate. In this case, it's total. Then we have the keyword case. Following the word case is the value of the variable we're looking for. So in the first set of this, we're saying essentially if total is one, echo total is one. We can have as many cases as we'd like here. Also note the break keyword. This is critical. Unlike an if statement, which exits as soon as it finds a true value, a switch statement will keep going unless you explicitly tell it to stop. The break statement explicitly tells it to stop. Finally, the default keyword is like the else in an if statement. It is a catchall for if we didn't match any of the other cases. You can also check for strings in switch statements like we have here. We have two variables, turtle and bandana, and we are using a switch statement on the keyword turtle. So if the case is turtle equals Leo, then bandana gets the value blue. With Raph, it's red, with Mikey, it's orange, with Don, it's purple, and the default is red. Note our break statements. Without those, the bandana would always be assigned red no matter what. And it's also worth noting that inside switch statements. we can have any number of expressions under case. So we have bandana gets blue and then break. We can have more in there as long as we put it before the break. One last note about switch statements is because the break statement is the thing that gets us out of the switch statement, we can actually group cases together. So in this case, we have total gets two, and in our switch statements, we have blank for case one through three, and then for case four, echo total is less than four, but more than zero, and then break. And then we have further cases after that. Now, let's look at match statements. Match statements are similar, but their syntax is compressed, and they are for assigning variables with multiple instances, so that we don't need to have a long switch statement or if statement. Let's look at our turtles example again. We have turtle equals Leo, or turtle gets Leo, and then our bandana is blank. Now, we have an assignment, bandana gets and then the keyword match, in parentheses, turtle, and then we have this array like, or a key value like assignment here where it's Leo arrow blue, Raph arrow red, Mikey arrow orange, Don arrow purple, and default, which note, is not in parent, rephrase. And default, which note, is not in quotes, is red. In most cases, the match statement will be better for you. It's a little bit more stricter, and therefore more predictable. But if you need multiple statements, or you're not just doing a straight assignment, the switch statement is better.
Contents
-
-
-
-
(Locked)
Comparison operators and the truth4m 26s
-
(Locked)
Logical operators5m 55s
-
(Locked)
Creating if/else statements6m 53s
-
Switch and match statements4m 28s
-
(Locked)
Conditional tags in WordPress1m 19s
-
(Locked)
Loops7m 56s
-
(Locked)
The Loop in WordPress2m 54s
-
(Locked)
Challenge: Loop through an array of information1m 12s
-
(Locked)
Solution: Loop through an array of information5m 28s
-
(Locked)
-
-
-