php flow control statement
Conditional control statements and loop control statements are two basic grammatical structures. They are used to control the execution flow of the program and are also the main grammatical basis of the program.
Three control structures of programs
Conditional control statement
Loop control statement
Jump statement
Contains statement
1. There are three types of programming structures: sequential structure, selection (branch) structure and loop structure.
The loop structure can repeatedly execute one or more lines of code as many times as needed. The loop structure is divided into two types: pre-test loop and post-test loop.
Pre-test loop, judge first and then execute,
Post-test loop, execute first and then judge.
Conditional control statements: if, slse, elseif and switch
Loop control statements: while, do...while, for and foreach
Jump control statements: break, continue and retun
2. Conditional control statement
The so-called conditional control statement is to judge the values of different conditions in the statement, and then execute different statements according to different conditions. There are two main statements in the conditional control statement: if conditional control statement and switch multi-branch statement.
1. The if conditional control statement is the simplest and most commonly used of all flow control statements. Different statements are executed based on the different conditions obtained
if(expr)
statement; //Basic expression
if(){} //Expression to execute multiple conditional statements
if(){}else{} //Extended expression through else
if(){}elseif(){}else{} //This is an expression that adds elseif to judge multiple conditions at the same time
//The parameter expr is evaluated according to Boolean. If it is true, the statement will be executed. If it is FALSE, the statement will be ignored. The if statement can be nested into other if statements infinitely to implement the execution of more conditions.
2. Switch multi-branch statement: The switch statement is similar to the if conditional control statement. It compares the same expression with many different values, obtains the same value, and executes the statement corresponding to the same value.
switch(expr){ //The value of the expression, that is, the name of the condition variable of the switch statement
case expr1; //Place it after the case statement and it is one of the values to be matched with the condition variable expr
Statement1; //When the condition matches, the code to be executed
Break; // Terminate the execution of the statement, that is, when the statement is executing, the master stops execution and jumps out of the loop
case exp2;
Statement2;
break;
default;
StatementN;
break;
}
3. Loop statement
A loop statement is to repeatedly perform an operation when a condition is met. In PHP, 4 loop control statements are provided, namely while loop statement, do..while, for, foreach loop
1. While loop statement, its function is to repeatedly perform an operation. It is the simplest one among the loop control statements and the most commonly used one. The while loop statement judges the value of the expression. When the expression is non-0 When , the embedded statements in the while statement are executed. When the value of the expression is 0, the embedded statements in the while statement are not executed. The characteristics of this statement are: the expression is judged first and then the statement is executed.
Example: while(expr){
Statement;
//As long as the value of expr of the while expression is TRUE, the nested statement statement will be executed repeatedly. If the value of the while expression is FALSE, the loop statement will not be executed once.
$a=1;
$b=10;
while ($a<=$b){
$p=40*12*$a;
echo "aaaa:".$a."bbbbb:".$p."
";
$a++;
}
Example:
do{
statement; //The program performs a loop before judging, and then judges the condition after looping to the while part. Even if the condition is not met, the program has already been run once.
}while(expr);
The difference between while and do..while statements: The do..while statement is executed first and then judged. Regardless of whether the value of the expression is TRUE, a loop will be executed, while the while statement first judges whether the value of the expression is TRUE. TRUE, if TRUE, the loop statement will be executed, otherwise the loop statement will not be executed.
3. The for loop statement is the most complex loop control statement in PHP. It has 3 conditional expressions and the syntax is as follows:
for(expr1;expr2;expr3){
statement
}
expr1 required parameter, the first conditional expression, is executed at the beginning of the first loop
expr2 is a necessary parameter, the second conditional expression, which is executed at the beginning of each loop to determine whether the loop continues
expr3 required parameter, the third conditional expression, is executed at the end of each loop,
statenebt Necessary parameters, after the conditions are met, the statement to be executed in a loop
Its execution process: first execute expression 1, then execute expression 2, and judge the value of expression 2. If the value is true, execute the embedded statement specified in the for loop statement. If the value is false, end Loop, jump out of the for loop statement, finally execute expression 3 (do not do it when the value of expression 2 is true), return expression 2 to continue loop execution,
4. foreach loop statement
The foreach loop control statement has been introduced since PHP 4. It is mainly used to process arrays and is a simple method to traverse arrays. If this statement is used to process other data types or initialized variables, an error will occur. The syntax of this statement has two formats:
foreach(array_expression as $key=>value){
statement
}
or
foreach(array_expression as $value){
statement
}
//array_expression specifies the array to be traversed, where $key is the key name of the array, $value is the value of the array, and statement is the statement to be executed in a loop when the conditions are met.
4. Jump statement
Jump statements are mainly divided into three parts: break statement, continue statement and return statement. The first two jump statements are very simple to use and easy to master. The main reason is that they are applied in specified environments, such as for loops. in the statement. The return statement is relatively simple in application environment compared to the former two, and is generally used in custom functions and object-oriented classes.
The break keyword can terminate the current loop, including all control statements including while, do..while, for, foreach and switch. The break statement can not only jump out of the current loop, but also specify how many loops to jump out of. The format is as follows: break n; Parameter n specifies the number of loops to jump out of.
The continue jump statement. After the program executes break, the program will jump out of the loop and continue to execute the subsequent statements of the loop body. The continue jump statement is not as powerful as break. It can only terminate this loop and enter the next loop. middle. After executing the contiue statement, the program will end the execution of this loop and start the next round of loop execution. continue can also specify how many loops to jump out of.
The break and continue statements both implement jump functions, but there is a difference. The continue statement only ends the current loop, not the execution of the entire loop, while the break statement ends the entire loop process and does not determine whether the conditions for executing the loop are established.
5. Contains the statement
Referencing external files can reduce code reusability,
When using the include() statement to include an external file, the external file will be included only when the code is executed to the statement. When an error occurs in the included external file, the system will only give a warning, and the entire php file will continue to execute downwards. Syntax: include(filename); filename is the specified full path file name.
The require() statement is similar to the include() statement, both implement calls to external files, the syntax require(filename); When using the require() statement to load a file, it will be executed as part of the php file, for example, through require() loads a web file, then any php commands in the file will be processed. However, if the php script is simply placed in an html web page, it will not be processed.
include_once() statement, the difference between include_once() and include() function, if you use include_once() function to call the same file multiple times, the program will only call it once. It is basically the same as the include function. The only difference is that the include_once function will check whether the file has been imported in other parts of the page before importing the file. If so, the file will not be imported repeatedly. This difference is very different. important. If you are importing some custom functions, there will be problems if you import them repeatedly.
The require_once() statement is an extension of require and has similar functions. The same meaning as the include_once() statement. If the require_once statement calls two identical files on the same page, only the first file will be output during output, and the file called for the first time will not be output.
The difference between include() and require() statements:
When the require() statement calls a file, if the file is not found, an error message will be output and the script processing will be terminated immediately. If the include() is not found, a warning will be issued and the script processing will not be terminated.
When the require() statement calls a file, the external file will be called immediately as soon as the program is executed. When the external file is called through the include() statement, the external file will only be called when the program executes the statement.
The difference between require_once() and include() statements: Their purpose is to ensure that an included file can only be included once, which can prevent accidental inclusion of the same function library multiple times resulting in repeated definitions of functions and errors, but The difference between the two is the same as that between require() and include().
Review:
1. Sequential structure,
2. Select (branch) structure
3. Loop structure
4. Learn 2 types of conditional control statements
5. Learn 4 types of loop control statements
6. Learn 3 types of jump statements
7. Contains statements, 2 types, should be 4 types, and their differences
Author "Technology is King"