和JSswitch的语法一样?
Switch Statements and the Ternary Operator are alternatives to if-else control structures for making decisions. The basic structure of a Switch Statement consists of the keyword switch followed by a pair of parentheses containing a value that you want to test. The body of the switch statement goes between a pair of curly braces. Inside the braces, the case keyword preceeds the value you‘re looking for and the value is followed by a colon.
If the value passed to the switch statment matches, any code that follows is executed. In effect, this is the same as a conditional statement using if, and testing whether var equals value one. And on its own, it‘s a rather compass approach which you can test for multiple values by adding break at the end of each case and repeating the pattern. At the end of the switch block, you can add default followed by a colon and any code that you want to be executed if none of the values match.
You can also stack alternative cases like this. The effect is exactly the same as this control structure using if, elseif, and else. There‘s no particular advantage in using one over the other. There‘s a switch statement can be easier to read and maintain if you need to test for many options. Let‘s take a quick look at the switch statement in operation. Here, the value that I want to test for is name. And if the name is Arthur, then this line of code will be executed. Break prevents any further code from being executed. But if the value is either Marvin or Paranoid Android, then this line of code will be executed.
Again, brake prevents the code from going any further. And if name doesn‘t match any of the values proceeded by the case keyword. Then the default is executed. So, if we test that in a browser. Because Arthur is the name, the first block of code is executed. Change the value of name to David. And refresh the browser. And this time, it doesn‘t match any of the case keywords.
So, the default is executed. And let‘s change it to Paranoid Android. And again, reload the browser. And we can see that it‘s matched this particular case. So, this line of code has been executed. But of course, the same line of code would have been executed if the name had been Marvin. Now, one of the things that you need to be very careful about with switch statements is if you forget to use break, the code continues executing until it gets to the end of the switch block, or it gets to another break. So, let‘s just try that.
Reloading the browser. And you see, we‘ve now got not only, I‘ve got this terrible pain of the diodes. But also, the last piece of code, the default is also being executed. So, it‘s extremely important when you are using switch statements, to make sure that each block of code ends with break. Unless, of course, you do want the default to be executed at the end anyway.
The other decision making structure is the Ternary Operator which assigns different values to a variable depending on whether a condition is true or false. It‘s structure looks like this.(跟JS还是一样的) The condition follows the assignment operator and is followed by a question mark. To the right of the question mark is the value you want assign if the condition is true. Next comes a colon, followed by the value if the condition is false. In effect, it‘s a short time way of writing this if, else statement.
Here‘s a simply example of the Ternary Operator in action. As every fan of the Hitchiker‘s Guide to the Galaxy will tell you, it took the computer deep thought seven and a half million years to work out that 42 was the answer to the ultimate question of life, the universe, and everything. So, if value isn‘t 42, well, let‘s see what the browser says. Indeed, keep calculating. So, if we change the value to 42, we know that the result is the answer to the ultimate question of life, the universe, and everything. So, there you have it, two alternatives to if, else conditional statements. Both can be difficult to understand in the early stages of working with PHP. But you‘ll find the Ternary Operator is a very convenient shorthand and is frequently used.
The switch statement is less common, but you definitely need to understand its basic structure and use.