Multi-branch in php refers to the PHP multi-directional conditional branch structure; in PHP, the elseif clause is a typical multi-directional conditional branch. It extends the if statement. The elseif clause will be based on different expression values. Determine which statement block to execute; in PHP, elseif can also be separated into two keywords else if for use.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3.
php What does multi-branching mean?
PHP branch control statement, branch structure of PHP process control structure
Flow control is universal and universal for any programming language, and is an important part of the program component. It can be said that in any programming language, three basic structures need to be supported: sequential structure, branch structure (selection structure or conditional structure) and loop structure. For sequential structures, mainly assignment statements and input/output statements, etc., that is, they are executed in order. There is nothing to say here. Here, the author focuses on summarizing the branch structure and loop structure.
Branch structure
The branch structure changes the order of program execution according to the required conditions during the execution of the program. That is, when the condition is met, a certain description block is executed, and otherwise, another description block is executed. The use of branch structures in programs can have the following forms:
1 2 3 4 |
|
Single conditional branch
The if structure is a single conditional branch structure, and the basic format of the if statement is An expression is evaluated, and based on the calculation result, it is decided whether to execute the following statements. The "expression" in parentheses after if is the condition for execution, and the result returned by the condition can only be a Boolean value. It is usually the result value calculated by an expression composed of comparison operators or logical operators, or some functions that return Boolean type, etc. If a value of other types is passed in, it will be automatically converted to Boolean TRUE or FALSE. If the expression is TRUE, the code block is executed, otherwise it is not executed.
DEMO
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Bidirectional conditional branch
Bidirectional conditional branch is like an if statement that can also contain an else clause, which means that it needs to satisfy a certain One statement is executed when a condition is met, and other statements are executed when the condition is not met. This is the function of the else clause. else extends the if statement and can execute the statement when the expression in the if statement evaluates to FALSE. It is worth noting that the else statement is a clause of the if statement and must be used together with if and cannot exist alone.
DEMO
1 2 3 4 5 6 7 8 9 10 |
|
Multi-directional conditional branch
The elseif clause is a typical multi-directional conditional branch. It extends the if statement. The elseif clause will Determine which statement block to execute based on different expression values. In PHP, elseif can also be separated into two keywords else if for use. The execution sequence is that if expression l is TRUE, then execute the code block 1 statement; if it is judged that expression 2 is TRUE, then execute the code block 2 statement; and so on, if the nth expression is judged to be TRUE, then the code block is executed. n statement; if the conditions of the expression are not TRUE, the code block n l statement in the else clause is executed. Of course, the last else statement can also be omitted.
DEMO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
The switch statement is similar to elseif and is also a multi-directional conditional branch structure, but if and elseif statements use Boolean expressions or Boolean values as branch conditions for branch control; and The switch statement is used to test the value of an expression and select and execute the corresponding branch program based on the test results to achieve branch control. The switch statement consists of a selection expression and multiple case labels. The case labels are followed by a code block. When using the switch statement, you should pay attention to the following points:
The data type of the selection expression after the switch statement can only be integer or string, not boolean. Usually this control expression is a variable name.
The 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 that follows it, and there must be a colon after the value, which is part of the syntax.
After the switch matching is completed, the statements in the matched branch modules will be executed one by one, and execution will not stop until the switch structure ends or a break statement is encountered.
The default label in the switch statement is directly followed by a colon, which means that the value of the expression cannot be equal to the value after any previous case label, and then default is executed. statement in the branch. The default tag can be omitted.
DEMO
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Nested conditional branch
The nested conditional branch structure is the nesting of if statements, which means that the code block after if or else contains if statement.
DEMO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does php multi-branch mean?. For more information, please follow other related articles on the PHP Chinese website!