PHP Process Control Statements
Conditional control statements and loop control statements are two basic syntactic constructs. They are used to control the execution of procedures, but also constitute the main grammatical basis of the program.
3 kinds of control structure of the program
Conditional control Statements
Loop control Statements
Jump statement
Include statement
1. The structure of program design is broadly divided into sequential structure, selection (branching) structure and cyclic structure 3 kinds.
Loop structure can be repeated as many times as necessary to execute one or more lines of code, the loop structure is divided into pre-test cycle and post-test cycle of two.
Pre-test cycle, first judgment after execution,
After the test-type cycle, the first execution after judgment.
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 Statements
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 statements in the conditional control statement: If condition control statement and switch multi-branch statement.
1. The IF Condition control statement is the simplest and most common one in all process control statements and executes different statements according to the different conditions obtained
if (expr)
Statement Basic expressions
if () {}//expression that executes a multi-condition statement
if () {}else{}//extends expression through else
if () {}elseif () {}else{}//This is an expression that joins the ElseIf to judge multiple conditions simultaneously
Parameter expr is evaluated in Boolean, if true, statement is executed, and if False, the Ignore STATEMENT,IF statement can be nested indefinitely into other if statements to implement more conditions.
2. Switch multi-branch statement: The switch statement is similar to the IF condition control statement, which compares the same expression to many different values, obtains the same value, and executes the same value for the corresponding statement.
The value of the switch (expr) {//expression, which is the name of the condition variable of the switch statement
Case EXPR1; After the case statement is placed, one of the values to match the condition variable expr
Statement1; Code that executes when a condition matches
Break Terminates the execution of the statement, that is, when the statement in the execution process, encountered a break main stop execution, jump out of the loop body
Case EXP2;
Statement2;
Break
Default case where any other cases do not match, and is the last one.
STATEMENTN;
Break
}
3. Circular statements
Loop statement, is to meet the conditions of the case of repeated execution of an operation, in PHP, provides 4 loop control statements, respectively, is the while loop statement, do. While,for,foreach Cycle
1.while Loop statement, which is the function of repeated execution of an operation, is the simplest of the loop control statement, is also the most common one, while the loop statement to determine the value of the expression, when the expression is non-0 o'clock, execute the inline statement in the while statement, when the value of the expression is 0 o'clock, The inline statement in the while statement is not executed. The characteristic of this statement is: First judge the expression, then execute the statement.
Example: while (expr) {
Statement
The statement statement in the nested is repeated as long as the value of expr for the while expression is true, and the loop statement does not execute once if the value of the while expression is false.
While Loop statement:
$a = 1;
$b = 10;
while ($a <= $b) {
$p =40*12* $a;
echo "AAAA:". $a. " BBBBB: ". $p."
";
$a + +;
}
2. Do. While loop statements are used in the same way as while, and are also output by judging the value of an expression. The procedure for this statement is to execute the specified loop statement once, then determine the value of the expression, and return to the loop body statement again, so repeated, when the value of the expression is not 0 o'clock. Until the value of the expression is equal to 0. The characteristic is that the loop body is executed first and then the loop condition is determined.
Cases:
do{
Statement The program does a loop before it is judged and loops to the while section to determine the condition, even if the condition is not met, and the program has run once.
}while (expr);
While and do: The difference between a while statement: do. The while statement executes a loop first, regardless of whether the value of the expression is true, the while statement is the first to determine whether the value of the expression is true, or if True the loop statement is executed, otherwise the loop statement is not executed.
3, for Loop statement, is the most complex in PHP loop control statements, with 3 conditional expressions, the syntax is as follows:
for (EXPR1;EXPR2;EXPR3) {
Statement
}
Expr1 necessary parameters, first conditional expression, executed at the beginning of the first loop
EXPR2 the necessary parameters, the second conditional expression, which is executed at the beginning of each loop, determines whether the loop continues
Expr3 necessary parameters, a third conditional expression, executed at the end of each loop,
Statenebt the necessary parameters, after satisfying the condition, the loop executes the statement
Its execution: first executes expression 1, then executes expression 2, and evaluates the value of expression 2, if the value is true, executes the inline statement specified in the FOR Loop statement, if the value is False, ends the loop, jumps out of the For Loop statement, and finally executes the expression 3 (avoid the value of expression 2 is true), Returns an expression of 2 to continue the loop execution,
4. Foreach Loop statement
The Foreach loop control statement, which is introduced from PHP4, is used primarily for working with arrays, and is a simple way to iterate through an array, resulting in an error if the statement is used to handle other data types or initialized variables. There are two forms of syntax for this statement:
foreach (array_expression as $key =>value) {
Statement
}
Or
foreach (array_expression as $value) {
Statement
}
Array_expression is the specified array to traverse, where $key is the key name of the array, $value is the value of the array, and statement is the statement to loop execution when the condition is met.
4. Jump Statement
The jump statement is divided into break statement, continue statement and return statement 3 parts, of which the first two jump statements are very simple to use, and very easy to master, mainly because they are applied in the specified environment, such as the FOR Loop statement. The return statement is relatively single in the application environment, and is typically used in custom functions and object-oriented classes.
The break keyword can terminate the current loop, including While,do. While,for,foreach and switch all control statements, the break statement can not only jump out of the current loop, you can also specify a few loops out of the format, such as: Break N; Parameter n Specifies the number of loops to jump out.
Continue jump statement, after the program execution break, the program will jump out of the loop, and start to continue to execute the loop body of the subsequent statements, continue jump statement function is not break so powerful, can only terminate this cycle, and enter into the next cycle. After executing the CONTIUE statement, the program ends the execution of the loop and begins the next loop of execution. Continue can also specify a few loops to jump out.
Break and continue statements are the functions that implement jumps, but there is a difference, the continue statement just ends the loop, not the execution of the entire loop, and the break statement is the end of the entire loop process, not to judge whether the condition of the execution loop is established.
5. Include statements
Referencing external files can reduce the reusability of code,
When using the include () statement to include an external file, the external file is included only when the code executes to the statement, and when an error occurs in the contained external file, the system only gives a warning, and the entire PHP file continues to execute downwards. Syntax: include (filename); FileName is the full path file name specified.
The Require () statement is similar to the include () statement, which implements the invocation of the external file, syntax require (filename); When a file is loaded with the Require () statement, it is executed as part of the PHP file, for example, by loading a net file with require (), and any PHP command within the file is processed, but it will not be processed if the PHP script is simply placed in an HTML page.
The include_once () statement, include_once () differs from the Include () function, and the application include_once () function calls the same file multiple times, and the program is only called once. He is basically the same as the include function, the only difference is that the Include_once function detects whether the file was imported in the other part of the page before importing it, and it is important that the file is not repeated when it is imported. If you import some custom functions, there will be a problem if you repeat the import.
The require_once () statement, which is an extension of require, is similar to his function. The same meaning as the include_once () statement. If the require_once statement invokes two identical files on the same page, only the first file is output when the output is made, and the first file is not exported.
The difference between the include () and require () statements:
When a file is called by the Require () statement, if the file is not found, the error message is output and the script processing is terminated immediately, and the include () is not found and a warning is issued, and the processing of the script is not terminated.
When the require () statement invokes the file, the external file is called immediately as soon as the program executes, and when an external file is called through the Include () statement, the external file is called only when the program executes to the statement.
The difference between the require_once () and include () statements: Their purpose is to ensure that a contained file can only be included once, which prevents accidental inclusion of the same library of functions and results in a duplicate definition of the function and produces an error, but both differences and require () The same as the include ().
Review:
1. Sequential structure,
2. Select (branch) structure
3. Cyclic structure
4.2 Types of conditional control statements
5.4 types of circular control statements
6.3 Types of Jump statements
7. Contains the statement, 2 kinds, should be 4 kinds, and their difference
The author "technology is king"
http://www.bkjia.com/PHPjc/478639.html www.bkjia.com true http://www.bkjia.com/PHPjc/478639.html techarticle PHP Process Control Statement Conditional control statements and loop control statements are two basic grammatical structures. They are used to control the execution of procedures, but also constitute the main grammatical basis of the program. ...