The content introduced in this article is about the control process of PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it
Flow control
What is process control
Control the order of program execution
Program structure
Sequential structure
The most basic structure, the program is executed from top to bottom
Branch structure
One-way branch structure
Format
if (conditional expression) {statement for successful execution of expression}
Note
You can specify true or false directly in the brackets, but it is meaningless
If the braces have only one statement, you can omit the braces
Bidirectional branch structure
Format
if (conditional expression) {statement to be executed when the expression is true}else{statement to be executed when the expression is not true}
Note
The else clause must be used with the if statement
Only one statement can omit the braces
If there are if and else you must execute the code in one of the brackets
Multi-directional branch structure
if(){}else if(){}....else{}
Format
if(conditional expression 1){
Code segment 1
}else if(conditional expression 2){
Code Section 2
} Else IF (Conditional expression 3) {
code segment 3
} else if ...
code segment n
Else {
The conditions above Things to be executed when neither is true
}
Note
1. The else clause can be omitted
2.The else if can be It is written as elseif but it is recommended that you write it as else if
3. If there is only one statement, you don’t need to add the braces
4. The if condition is executed from top to bottom, and among them are executed The code segment at one point will not be checked and executed further
5. This branch structure is usually used to determine the range condition
switch multiple branch structure
Format
switch(variable){
case value:
Statement 1
break; Statement 2
break;
case value n:
statement n
break;
default:
Statement
break;
##Note
1. The switch bracket must be a variable or expression, but usually a variable is placed (unexpected things will happen when placing the expression Error)
2. Within switch(){} curly brackets, any number of statements are placed. The value is placed after the space in case. Use a colon after the value. Write the statement to be executed and finally use break to exit.
3. If the value after case is a string, it must be quoted
4. The break after the case is optional. The function of break is to exit. There is a break in each case because it exits after executing the case. If there is no break, the case will exit from the first case. A place that can be matched is executed downward until it encounters break and exits
5. The switch structure is mainly used to match multiple values
Nested branch structure The nested branch structure is a comprehensive nesting of the previous branch structures
Loop structure
What is the loop structure
There are some codes in the program that need to be executed multiple times. At this time, the loop structure is used
Classification
while loop
While (conditional expression) {
}
# One-step restrictive conditional expression. When the expression is true, the code in the loop structure will be executed.
Note
1. While can be written as true, but it will Generating an infinite loop (infinite loop)
2. You can write loops inside the loop but do not exceed three levels
3. While loops are usually used to make conditional loops
do while loop
Format
do{
Code for repeated execution
}while(conditional expression);
Explanation
The first step is to execute the statement in do first, and then check the conditional expression Whether the formula is established, if it is true, the code in do will continue to be executed, if it is false, it will not be executed.
Note
1. No matter what, the do while loop will be executed at least once
2. There must be a semicolon after while
3. Suitable for conditional loops
for loop
Format
for(expression Expression 1;Expression 2;Expression 3){ ’ ’ ’ ’ ’ s ’ ’s ’ ’s ’ s ’ through ’ through through ’s ’ through ’ s through through ’s ’ through ’ through ’ through ’ through through ‐ to ‐ ‐w‐ww to Formula 3 is a self-increasing or self-decreasing (counting condition)
Description
First: execute expression 1
first Two: Execute expression 2
Third: If expression 2 is true, execute the loop statement and execute expression 3 Fourth: Execute expression 2, if the condition is true, repeat step three Five: If expression 2 does not hold, the loop ends
Note
1. Expression 1 is only executed once
2. for loop Usually used for counting loops
3. The for loop can actually be similar to the while structure
Expression 1
for(; Expression 2;){
Expression 3 }
You need to understand the format of the for loop and the while Almost
4. Expression 1 and Expression 3 can have multiple values. Use commas to separate multiple values.
5. Expression 3 can not only Use or -- you can also use other operatorsWhat is flow control
Control the order of program execution
Program structure
Sequential structure
The most basic structure, the program is executed from top to bottom
Branch structure
One-way branch structure
Format
if (conditional expression) {statement for successful execution of expression}
Note
can be specified directly in parentheses true or false, but meaningless
If the braces have only one statement, you can omit the braces
Bidirectional branch structure
Formatif (conditional expression) {statement executed when the expression is true}else {statement executed when the expression is not true}
Note
else clause must be used with if statement
Only one statement can omit the curly brackets
If there are if and else in the program, one of the brackets must be executed Code
Multi-directional branch structure
if(){}else if(){}....else{}
Format
IF (Condition Expressive 1) {
code segment 1
} else if (conditional expression 2) {
code segment 2
} else if ( Conditional expression 3){
use using using use use using using using ’ s through using using using through out through out through out through out through out out through through through ’'s' ‐ ‐ to ‐ ‐‐‐‐ and ‐ to. # }
Note
1. The else clause can be omitted
2. Else if can be written as elseif but it is recommended that you write it as else if
3. If there is only one statement, you don’t need to add braces
4. The if condition is executed from top to bottom. If one of the code segments is executed, it will not Then check and execute
5. This branch structure is usually used to determine the range condition
switch multiple branch structure
Format
switch(variable){ case value: Statement 1
break; Statement 2 break;
use use case ’s use ’s using using ’ s ’ through using ’ s ’ ’ ’‐ ‐ ‐ ‐ n , ‐ to break;
}
##Note
1. The switch bracket must be a variable or expression, but usually a variable is placed (unexpected errors will occur when placing the expression)
2. Within switch(){} braces, any number of statements are placed. The value is placed after the space in case. Use a colon after the value. Write the statement to be executed and finally use break to exit.
3. If the value after case is a string, it must be quoted
4. The break after the case is optional. The function of break is to exit. There is a break in each case because it exits after executing the case. If there is no break, the case will exit from the first case. A place that can be matched is executed downward until it encounters break and exits
5. The switch structure is mainly used to match multiple values
Nested branch structure
The nested branch structure is to use the previous branch structure to comprehensively nest it
Loop structure
What is the loop structure
There are some codes in the program that need to be executed multiple times. At this time, the loop structure is used
Classification
while loop
Format
while (conditional expression){ Write the code to be repeated
}
Description
The first step is the restrictive condition expression. When the expression is true, the code in the loop structure will be executed.
Note1. You can write true in while, but it will produce an infinite loop (infinite loop)
2. You can write loops in the loop but do not exceed three levels
3. While loop is usually used to make conditional loop
do while loop
Format
do{
ken temperature Soundstimanti then then then to you you you you you so ##The first step is to execute the statement in do, and then check whether the conditional expression is established. If it is true, continue to execute the code in do. If it is false, it will not be executed.
Note
1. No matter what, the do while loop will be executed at least once
2. There must be a semicolon after while
3. Suitable for conditional loops
for loop
Format
for(expression Expression 1;Expression 2;Expression 3){ ’ ’ ’ ’ ’ s ’ ’s ’ ’s ’ s ’ through ’ through through ’s ’ through ’ s through through ’s ’ through ’ through ’ through ’ through through ‐ to ‐ ‐w‐ww to Formula 3 is a self-increasing or self-decreasing (counting condition)
Description
First: execute expression 1 first Two: Execute expression 2 Third: If expression 2 is true, execute the loop statement and execute expression 3
Fourth: Execute expression 2, if the condition is true, repeat step three
Five: If expression 2 does not hold, the loop ends
Note
1. Expression 1 is only executed once
2. for loop Usually used for counting loops
3. The for loop can actually be similar to the while structure
Expression 1
for(; Expression 2;){
Expression 3 }
You need to understand the format of the for loop and the while Almost
4. Expression 1 and Expression 3 can have multiple values. Use commas to separate multiple values.
5. Expression 3 can not only Use or -- you can also use other operators
Related recommendations:
php control statements
The above is the detailed content of php control flow. For more information, please follow other related articles on the PHP Chinese website!