Implementation analysis of PHP basic learning flow control

  • 2020-06-01 08:27:26
  • OfStack

PHP has three major process controls: sequence control, branch control, and loop control.

1. Sequence control: it is the execution step by step from top to bottom in order.

2. Branch control: selective execution of the program. It is divided into single branch, multiple branch and multiple branch.

a, single branch: basic grammatical structure:

if(conditional expression){

Statements;

/ /... ;

Hint: the conditional expression, no matter how complex, ends up being true or false;

eg:

a=11;

if(a > 10){

echo a" > 10";

}

b, multiple branches: basic grammar:

if(conditional expression){

Statements;

/ /... ;

}else{

Statements;

/ /... ;

}

c, multiple branches: basic syntax:

if(conditional expression){

Statements; n statements;

}else if(conditional expression){

Statements; n statements;

}elseif(conditional expression){

Statements; n statements;

}eles{

Statements; n statements;

1. else if can have one or more. 2. The last else can be eliminated

d, switch branch statements

switch(expression){

case constant 1:

Statements; n statements;

break;

case constants 2:

Statements; n statements;

break;

case constants 3:

Statements; n statements;

break;

default:

Statements; n statements;

break;

} to note:

1, case statements have 1 to more 2, defaul statement can not (according to the business logic of their own code) 3. In general, break is used after case to exit switch 4. Types of constants (int, float, string, Boolean)

Key points: firstly, the program is configured in order of case. If none of them is matched, the content of default statement will be executed, and switch will be exited when break is encountered.

if and switch branches:

if's judgment of a certain range, while switch's judgment of a point, so we can select them as follows:

Application scenario: swtich should be used when our branch is a few points (for example, to judge the direction of the tank), and if should be considered if your branch is a few areas (areas)

Related articles: