PHP has three major process controls: sequential control, branch control, and cyclic control.
1. Sequential Control: YesProgramPerform the preceding steps in sequence.
2. Branch Control: the program can be executed selectively. It can also be divided into single branch, multiple branch, and multiple branch.
A. Single Branch: basic syntax structure:
If (conditional expression ){
Statement;
//.....;
} Tip:The conditional expression is true or false no matter how complex it is;
Eg:
A = 11;
If (A> 10 ){
Echo "A> 10 ";
}
B. Multi-branch: basic syntax:
If (conditional expression ){
Statement;
//.....;
} Else {
Statement;
//.....;
}
C. Multiple branches: basic syntax:
If (conditional expression ){
Statement; n statements;
} Else if (conditional expression ){
Statement; n statements;
} Elseif (conditional expression ){
Statement; n statements;
} Eles {
Statement; n statements;
} Tip:1. Else if can have one or more. 2. The last else can have no
D. Switch branch statement
Switch (expression ){
Case constant 1:
Statement; n statements;
Break;
Case constant 2:
Statement; n statements;
Break;
Case constant 3:
Statement; n statements;
Break;
Default:
Statement; n statements;
Break;
} Note:
1. One or more case statements 2. defaul statement can be absent (based on your own)CodeBusiness Logic) 3. Generally, after a case statement, a break is required to exit the switch statement. 4. Constant type (INT, float, String, Boolean)
Important:The program is first configured in the order of case. If none of them match, the program executes the content of the default statement until it encounters a break and then exits the switch;
If and switch branch comparison:
If is used to determine a certain range, and switch is used to determine a point, we can select them as follows:
Application Scenario: When the branches are just a few points (for example, to determine the direction of the tank), swtich should be used. If your branches are in several regionsIf