Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use switch in php

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use switch in php". The content in the article 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 in php.

In php, the switch statement is used to perform different actions according to several different conditions. The syntax "switch (expression) {case value 1: statement block 1 leading breakaway. Case value n: statement block n: statement block n;}"; the value of an expression can only be an integer or a string.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP Switch statement

The switch statement is used to perform different actions based on several different conditions.

The switch statement consists of an expression and multiple case tags, the case tag followed by a code block, and the case tag as the identification of this code block. The syntax format of the switch statement is as follows:

Switch (expression) {case value 1: statement block 1; break; case value 2: statement block 2; break;. Case value n: statement block n; break; default: statement block nail1;}

The switch statement compares the value of the expression with the value in case in turn, and if it is not equal, it continues to find the next case;. If it is equal, it executes the corresponding statement until the end of the switch statement or until break is encountered.

In general, switch statements end up with a default value of default, and if no matching condition is found in the previous case, the default statement is executed, similar to the else statement.

The execution flow of the switch statement is shown in the following figure:

The following points should be noted when using switch statements:

Unlike the if statement, the data type of the expression after the switch statement can only be an integer or a string, not a bool. Usually this expression is a variable name, although PHP is a weakly typed language, and the variable of the expression after switch can be any type of data, but in order to ensure the accuracy of matching execution, it is best to use only one type in the integer or string.

Unlike the if statement, curly braces after the switch statement are required.

The number of case statements is not specified and can be increased indefinitely. But there should be a space between the case tag and the value after the case tag, and the value must be followed by a colon, which is part of the syntax.

After the switch matching is complete, the statements in the matching branch module are executed one by one until the end of the switch structure or the break statement is encountered. So, if a break statement is not written at the end of a branch statement, the program will continue to execute the contents of the next branch statement.

Similar to the else in the if statement, the default tag in the switch statement is directly followed by a colon, which seems to have no condition, but it is conditional on the condition that the value of the "expression" cannot be equal to the value after any previous case tag, and then the statement in the default branch is executed. The default tag, like the else clause in if, is not required in the switch statement and can be omitted.

[example] use the date () function to get the abbreviation of the current week, and print what day it is today according to the abbreviation. The code is as follows:

The running results are as follows:

Thank you for your reading, the above is the content of "how to use switch in php", after the study of this article, I believe you have a deeper understanding of how to use switch 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report