PHP's Control Flow

Source: Internet
Author: User
This article introduces the content of the PHP control process, has a certain reference value, now share to everyone, the need for friends can refer to

Process Control
What is Process Control
Sequence of execution of the control program
Program Structure
Sequential structure
The most basic structure, the program is executed from top to bottom
Branching structure
One-way branching structure
Format
if (conditional expression) {expression successfully executed by statement}
Attention
You can specify TRUE or false directly in parentheses, but there is no meaning
If the curly brace has only one statement, you can omit the curly brace
Bidirectional branching structure
Format
if (conditional expression) {The statement executed when the expression is formed}else{the statement executed when the expression is not true}
Attention
Else clause must be used in conjunction with the IF statement
Only one statement can omit curly braces
If there are if and else in the program, you must execute the code in one of the parentheses
Multidirectional branching structure
if () {}else if () {}....else{}
Format
if (conditional expression 1) {
Code Snippet 1
}else if (conditional expression 2) {
Code Snippet 2
}else if (conditional expression 3) {
Code Snippet 3
}else If ...
Code Snippet N
else{
The above conditions are not set when the execution of something
}
Attention
1, else clauses can be omitted
2, else if can be written as ElseIf but it is recommended that you write the else if
3, if a statement can not increase the parentheses
4. If condition is executed from top to bottom, execution of one of the code snippets will no longer be checked for execution
5. This branch structure is usually used to determine the range conditions
Switch Multi-branch structure
Format
Switch (variable) {
Case Value:
Statement 1
Break
Case Value 2:
Statement 2
Break


Case Value N:
Statement n
Break
Default
Statement
Break
}


Attention
1, switch parentheses, must be a variable or expression, but usually put a variable (when placing an expression, there will be unexpected errors)
2, switch () {} curly braces, placed in any number of statements. The case space is followed by a value, followed by a colon, followed by the statement to be executed, and finally using break to exit
3, the value after the case if it is a string, be sure to enclose the quotation marks
4. The break after the case is optional, the function of break is to exit, each case inside a break is due to exit after executing the box, if the non-break is not a break from the first place can be executed until the break exit
5, switch structure is mainly used to match multiple values of the case
Nested branching structure
Nested branching structure is the former branch structure, integrated nesting use
Loop structure
What loop structure?
There is some code in the program that needs to be repeated several times, and the loop structure is used.
Classification
While loop
Format
while (conditional expression) {
Write the code that you want to repeat
}
Description
The first restriction conditional expression, when the expression is true, executes the code in the loop structure
Attention
1, while inside can be written as true, but will produce a dead loop (infinite loop)
2, the loop can be in the write cycle but not more than three layers
3. While loops are usually used to make conditional loops
Do and loop
Format
do{


Code that executes repeatedly


}while (conditional expression);


Description
The first step executes the statement in the do, and then checks whether the conditional expression is true, and then resumes execution of the code in do if False is not executed.
Attention
1. Do and loops will be executed at least once at any time.
2. There must be a semicolon after the while
3, suitable for the condition cycle
For loop
Format
for (expression 1; expression 2; expression 3) {
The Code of the Loop
}


Expression 1 is the condition of the initialization
Expression 2 is Conditional
Expression 3 is a self-increment or decrement (count condition)


Description
First: Execute an expression 1 first
Second: Execution expression 2
Third: If expression 2 is true, execute the loop statement, execute the expression 3
IV: Execution Expression 2, if the condition is set again repeat step three
V: If expression 2 is not true, the loop ends
Attention
1. Expression 1 executes once
2, for loop is usually used to do counting cycle
3, for loop can be similar to the while structure
Expression 1


for (; expression 2;) {


Expression 3
}


In this notation you need to understand that the for loop format is the same as while
4, expression 1 and expression 3 can have multiple values, separated by commas between multiple values
5, Expression 3 can not only use + + or--can also use other operators
What is Process Control
Sequence of execution of the control program
Program Structure
Sequential structure
The most basic structure, the program is executed from top to bottom
Branching structure
One-way branching structure
Format
if (conditional expression) {expression successfully executed by statement}
Attention
You can specify TRUE or false directly in parentheses, but there is no meaning
If the curly brace has only one statement, you can omit the curly brace
Bidirectional branching structure
Format
if (conditional expression) {The statement executed when the expression is formed}else{the statement executed when the expression is not true}
Attention
Else clause must be used in conjunction with the IF statement
Only one statement can omit curly braces
If there are if and else in the program, you must execute the code in one of the parentheses
Multidirectional branching structure
if () {}else if () {}....else{}
Format
if (conditional expression 1) {
Code Snippet 1
}else if (conditional expression 2) {
Code Snippet 2
}else if (conditional expression 3) {
Code Snippet 3
}else If ...
Code Snippet N
else{
The above conditions are not set when the execution of something
}
Attention
1, else clauses can be omitted
2, else if can be written as ElseIf but it is recommended that you write the else if
3, if a statement can not increase the parentheses
4. If condition is executed from top to bottom, execution of one of the code snippets will no longer be checked for execution
5. This branch structure is usually used to determine the range conditions
Switch Multi-branch structure
Format
Switch (variable) {
Case Value:
Statement 1
Break
Case Value 2:
Statement 2
Break


Case Value N:
Statement n
Break
Default
Statement
Break
}


Attention
1, switch parentheses, must be a variable or expression, but usually put a variable (when placing an expression, there will be unexpected errors)
2, switch () {} curly braces, placed in any number of statements. The case space is followed by a value, followed by a colon, followed by the statement to be executed, and finally using break to exit
3, the value after the case if it is a string, be sure to enclose the quotation marks
4. The break after the case is optional, the function of break is to exit, each case inside a break is due to exit after executing the box, if the non-break is not a break from the first place can be executed until the break exit
5, switch structure is mainly used to match multiple values of the case
Nested branching structure
Nested branching structure is the former branch structure, integrated nesting use
Loop structure
What loop structure?
There is some code in the program that needs to be repeated several times, and the loop structure is used.
Classification
While loop
Format
while (conditional expression) {
Write the code that you want to repeat
}
Description
The first restriction conditional expression, when the expression is true, executes the code in the loop structure
Attention
1, while inside can be written as true, but will produce a dead loop (infinite loop)
2, the loop can be in the write cycle but not more than three layers
3. While loops are usually used to make conditional loops
Do and loop
Format
do{


Code that executes repeatedly


}while (conditional expression);


Description
The first step executes the statement in the do, and then checks whether the conditional expression is true, and then resumes execution of the code in do if False is not executed.
Attention
1. Do and loops will be executed at least once at any time.
2. There must be a semicolon after the while
3, suitable for the condition cycle
For loop
Format
for (expression 1; expression 2; expression 3) {
The Code of the Loop
}


Expression 1 is the condition of the initialization
Expression 2 is Conditional
Expression 3 is a self-increment or decrement (count condition)


Description
First: Execute an expression 1 first
Second: Execution expression 2
Third: If expression 2 is true, execute the loop statement, execute the expression 3
IV: Execution Expression 2, if the condition is set again repeat step three
V: If expression 2 is not true, the loop ends
Attention
1. Expression 1 executes once
2, for loop is usually used to do counting cycle
3, for loop can be similar to the while structure
Expression 1


for (; expression 2;) {


Expression 3
}


In this notation you need to understand that the for loop format is the same as while
4, expression 1 and expression 3 can have multiple values, separated by commas between multiple values
5, Expression 3 can not only use + + or--can also use other operators

Related recommendations:

Control statements for PHP

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.