From the course: Learning PHP
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Switch statements
- [Instructor] An alternative way of decision making is switch statements. Where if statements allow you to check to see if a condition is true, switch statements allow you to more clearly make decisions based on the value of a single variable. Here's a look at the syntax. Let's say we have a variable called total that has the value 10. We start with the keyword switch, followed by parentheses, and then the variable we're looking to evaluate, in this case, total. We have our curly braces and then we have a case. The keyword case is basically saying, does our variable in question equal the following value? So we have switch total case one is saying is total one. If it is, we have a colon and then the code we want to execute. In this case, we're printing out that total is one. And then we have a break statement. More on that later. But we can continue to have as many case statements as we want. So in this case we…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Boolean operators and the truth (update for PHP 8.0)6m 27s
-
(Locked)
Logical operators6m 34s
-
(Locked)
Creating if/else statements8m 5s
-
(Locked)
Ternary operations5m 53s
-
(Locked)
Switch statements7m 8s
-
(Locked)
Match expression8m 4s
-
(Locked)
Challenge: Display content based on status1m
-
(Locked)
Solution: Display content based on status5m 1s
-
-
-
-
-
-
-
-
-