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

Switch statements - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

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…

Contents