Php process control statement _ PHP Tutorial

Source: Internet
Author: User
Php process control statement. Php process control statements conditional control statements and cyclic control statements are two basic syntax structures. They are all used to control the program execution process and constitute the main syntax basis of the program. Php process control statement

Conditional control statements and cyclic control statements are two basic syntax structures. They are all used to control the program execution process and constitute the main syntax basis of the program.

Three control structures of the program
Conditional control statement
Loop control statement
Jump statement
Include statement

1. the structure of the program design is roughly divided into sequential structure, and three types are selected (branch) structure and cyclic structure.

The loop structure can execute one or more lines of code multiple times as needed. The loop structure can be divided into pre-test loop and post-test loop.
Pre-test cycle: Judge before execution,
Then the test cycle is executed and then judged.

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 main statements in the conditional control statement: if condition control statement and switch multi-branch statement.

1. if condition control statements are the simplest and most commonly used process control statements. different statements are executed based on the obtained conditions.
If (expr)
Statement; // Basic expression
If () {}// the expression for executing a multi-condition statement
If () {} else {} // extends the expression through else
If () {} elseif () {} else {} // This is an expression that adds elseif to Judge multiple conditions at the same time.

// The expr parameter is Boolean. if it is true, statement is executed. if it is FALSE, statement is ignored. the if statement can be infinitely nested into other if statements, execute more conditions.


2. switch multi-branch statement: the switch statement is similar to the if condition control statement, which compares the same expression with many different values, obtains the same value, and executes the statement corresponding to the same value.

Switch (expr) {// expression value, that is, the name of the condition variable of the switch statement
Case expr1; // after the case statement, it is one of the values that must match the condition variable expr.
Statement1; // code executed when conditions match
Break; // terminate the execution of a statement, that is, when the statement is executed, the break master stops executing and jumps out of the loop body.
Case exp2;
Statement2;
Break;
Default; // A special case of case. the case does not match any other cases and is the last case statement.
StatementN;
Break;
}

3. loop statements

A loop statement is used to repeatedly execute an operation when the conditions are met. in php, four loop control statements are provided, namely the while loop statement, do .. while, for, foreach loop

1. while loop statements are used to execute an operation repeatedly. they are the simplest and most commonly used loop control statements. while loop statements are used to judge the value of expressions, if the expression is not 0, the embedded statement in the while statement is executed. if the expression value is 0, the embedded statement in the while statement is not executed. This statement is characterized by first determining the expression and then executing the statement.
Example: while (expr ){
Statement;
} // As long as the value of the while expression expr is TRUE, the statement in the nested statement will be executed repeatedly. if the value of the while expression is FALSE once, the loop statement will not be executed once.

While loop statement:
$ A = 1;
$ B = 10;
While ($ a <= $ B ){
$ P = 40*12 * $;
Echo "aaaa:". $ a. "bbbbb:". $ p ."
";
$ A ++;
}

2. the use of do... while loop statements is similar to that of while statements. it is also used to determine the value of an expression to output loop statements. The operation flow of this statement is: execute a specified loop statement first, and then judge the expression value. if the expression value is not 0, return to re-execute the loop body statement. Until the expression value is equal to 0. The feature is to first execute the loop body and then determine whether the loop condition is true.

Example:
Do {
Statement; // The program executes a loop before judgment, and the while part is used to judge the condition. even if the condition is not met, the program has run once.
} While (expr );

While and do .. difference Between while statements: do .. the while statement is executed first and then determined. a loop is executed no matter whether the expression value is TRUE or not, while the while statement first checks whether the expression value is TRUE, if it is TRUE, a loop statement is executed. Otherwise, a loop statement is not executed.

3. for loop statements are the most complex loop control statements in php and have three conditional expressions. The syntax is as follows:

For (expr1; expr2; expr3 ){
Statement
}

Required parameter of expr1. The first conditional expression is executed at the beginning of the first loop.
Expr2 is a required parameter. The second conditional expression is executed at the beginning of each loop to determine whether the loop continues.
Expr3 is a required parameter. The third conditional expression is executed at the end of each loop,
Statenebt required parameters. statements that are executed cyclically after the conditions are met

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, the loop ends, jumps out of the for loop statement, and finally executes expression 3 (not when the value of expression 2 is true). expression 2 is returned to continue the loop,

4. foreach loop statement
The foreach loop control statement is introduced from php4 and is mainly used to process arrays. it is a simple method to traverse arrays. if you use this statement to process other data types or initialize variables, errors will occur. The syntax of this statement can be in two formats:
Foreach (array_expression as $ key => value ){
Statement
}
Or
Foreach (array_expression as $ value ){
Statement
}

// Array_expression indicates the array to be traversed. $ key indicates the key name of the array, $ value indicates the value of the array, and statement indicates the statement to be executed cyclically when conditions are met.

4. jump statement

The jump statement mainly consists of the break statement, the continue statement and the return statement. The first two jump statements are very easy to use and are easy to understand, the main reason is that they are all applied in the specified environment, such as for loop statements. The return statement is relatively simple in the application environment than the previous two. it is generally used in user-defined functions and object-oriented classes.

The break keyword can terminate the current loop, including while, do .. for all control statements including while, for, foreach, and switch, the break statement can not only jump out of the current loop, but also specify the number of loops to jump out, in the format of break n; parameter n specifies the number of loops to be jumped out.

After the program executes the break, the program jumps out of the loop and continues to execute subsequent statements of the loop body. the function of the continue jump statement is not as powerful as that of the break, and only the cycle can be terminated, in the next loop. After the contiue statement is executed, the program ends the execution of this loop and starts the execution of the next loop. You can also specify the number of loops to be jumped out.

The break and continue statements are both jump functions, but there is another difference: the continue statement only ends the current loop, rather than terminating the execution of the entire loop, while the break statement ends the entire loop process, it does not determine whether the execution cycle conditions are true.

5. include statements

Referencing external files can reduce code reusability,

When an include () statement is used to include an external file, the external file is included only when the code executes this statement. When an error occurs for the contained external file, the system only gives a warning, the entire php file continues to be executed. Syntax: include (filename); filename is the specified complete path file name.

Similar to the include () statement, the require () statement is used to call external files. The syntax is require (filename). when the require () statement is used to load files, it will be executed as part of the php file. for example, if you load a file through require (), any php commands in the file will be processed. However, if you put the php script in an html webpage, it will not be processed.

The difference between the include_once () statement, include_once () and include () functions is that the program calls the same file multiple times using the include_once () function. Similar to the include function, the only difference is that the include_once function checks whether the file has been imported into other parts of the page before importing the file, if yes, the file will not be imported again. This area is also very important. If you are importing some custom functions, it may be a problem if you import them repeatedly.

The require_once () statement is an extension of require, similar to its function. The same as the include_once () statement. If the require_once statement calls two identical files on the same page, only the first file is output, and the file called for the first time is not output.

The difference between include () and require () statements:

When the require () statement calls a file, if the file is not found, an error message is output and the script processing is terminated immediately. if the include () statement is not found, a warning is issued, script processing will not be terminated.
When the require () statement calls a file, as long as the program is executed, the external file will be called immediately. when the include () statement is used to call an external file, only when the program executes this statement, to call an external file.

The difference between require_once () and include () statements: they are used to ensure that a contained file can only be included once, this prevents repeated function definitions and errors due to multiple accidental inclusion of the same function library, but the difference between the two is the same as that between require () and include.


Review:

1. ordered structure,
2. select the (branch) structure
3. loop structure
4. two types of conditional control statements
5. four types of cyclic control statements
6. three types of jump statements
7. the two types of statements should be four, and their differences

Author "technology is King"

Conditional control statements and cyclic control statements are two basic syntax structures. They are all used to control the program execution process and constitute the main syntax basis of the program ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.