In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use switch sentences in PHP". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use switch sentences in PHP".
PHP switch statement
Switch statement and if-else statement are similar, both perform different actions according to different conditions, but the efficiency of if-else statement judgment is low, we have a better way, that is, switch statement. If we want to be able to use the switch statement, we need to know its basic syntax format.
The switch statement consists of an expression and multiple case tags, as an example:
Switch (expression) {case condition 1: action 1; break; case condition 2: action 2; break; default:// does not meet conditions 1 and 2 but the remaining conditional action 3;}
Executing the switch statement first evaluates the expression, then compares the values in the expression with the values in case in order, executes its corresponding action or statement if they are equal, continues to compare the next case if they are not equal, and so on until the end of the switch statement or break is encountered.
Generally speaking, there is a default value at the end of the switch statement, that is, default in the above formula. If no matching condition is found in the previous case, the default statement, that is, default, will be executed. This is somewhat similar to the else statement.
At the same time, we should also pay attention to some things when using the switch statement:
The number of case statements can increase all the time. However, there should be a space between the case tag and the value after the case tag. Do not write a semicolon (;) after the case, followed by a colon (:).
Do not write the judgment interval after case, such as ($a > 2 or $a = = 3)
In switch, the variables are preferably integers, strings, not Boolean, because Boolean judgment is more suitable for if...else...
If you remove the break from each case, the blocks of code in each case will be executed in turn.
The switch statement does not have to write default, but in order to form a good habit, it is recommended to keep the default statement.
Unlike the if statement, the switch statement must be followed by curly braces.
Output result:
Output: study hard and make progress every day; output case actions that meet the conditions of the expression through the switch statement.
Another example is:
Output result:
Since no matching condition was found in the previous case, the default statement, default, is executed. So the output is default: unknown.
Thank you for your reading, the above is the content of "how to use switch statements in PHP". After the study of this article, I believe you have a deeper understanding of how to use switch statements in PHP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.