Home >Backend Development >PHP Tutorial >PHP basic flow control statements
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; }Tips: 1. Else if can have one or more. 2. The last else can be omitted 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. There are one to many case statements 2. The defaul statement does not need to be included (according to the business logic of your own code) 3. Usually, after the case statement, break is required to indicate exiting the switch statement. 4. Type of constant (int, float, string, Boolean) Key point: The program is first configured in case order. If none is matched, the contents of the default statement will be executed until break is encountered, and then the switch will exit; Comparison of if and switch branches: if judges a certain range, and switch judges a point, so we can select them like this: Application scenario: When the branches are just a few points (such as determining the direction of a tank), use switch. When the branch is a judgment of several areas (ranges), consider using if. |