Several methods of PHP loop control statements are described in detail. In php, loop statements generally use while, for, and foreach, and control statements are ifswicth. let's introduce the usage of php loop control statements. 1. if .. else loop has Loop statements in php generally use the while, for, and foreach statements, and the control statements are the if swicth statements. let's introduce the usage of php loop control statements.
1. the if... else loop has three structures. The first one is to use the if condition as a pure judgment.
It is interpreted as "how to deal with something ".
The syntax is as follows: if (expr) {statement} where expr is the condition for judgment. it is usually determined by a logical operator number.
Statement is the execution part of the program that meets the conditions. if the program has only one line, you can omit braces {}.
Example: braces are omitted in this example.
The code is as follows: |
|
If ($ state = 1) Echo "haha "; ?>
|
It is important to note that, to determine whether the equality is equal to = rather than =, ASP programmers may often make this mistake, = is a value assignment.
Example: the execution part of this example has three rows and the braces cannot be omitted.
The code is as follows: |
|
If ($ state = 1 ){ Echo "haha; Echo" "; } ?>
|
In addition to the if clause, the first two conditions of else can be interpreted as "what to do if something happens, otherwise how to solve it ".
Syntax:
The code is as follows: |
|
If (expr ){ Statement1 } Else { Statement2 }
|
Example: modify the preceding example to a more complete process.
Because else only executes one line of commands, no braces are added.
The code is as follows: |
|
If ($ state = 1 ){ Echo "haha "; Echo" "; } Else { Echo "haha "; Echo" "; } ?>
|
The third type is the recursive if... else loop, which is usually used for multiple decision making decisions.
It combines several if... else values for processing.
Let's look at the example below.
The code is as follows: |
|
If ($ a> $ B ){ Echo "a is larger than B "; } Elseif ($ a ==$ B ){ Echo "a equals B "; } Else { Echo "a is smaller than B "; } ?>
|
In the above example, we only use the layer-2 if .. else loop to compare the two variables a and B.
Actually, we need to use this recursion if .. please be careful when using else loops, because too many layers of loops may cause problems in the design logic, or if you leave less braces, the program may encounter inexplicable problems.
2. there is only one for loop without any change. Its syntax is as follows:
For (expr1; expr2; expr3) {statement}
Expr1 is the initial value of the condition.
Expr2 is a judgment condition, which is usually determined by using a logical operator number (logicaloperators.
Expr3 is the part to be executed after statement is executed. it is used to change the condition for the next loop judgment, such as adding one.
Statement is the execution part of the program that meets the conditions. if the program has only one line, you can omit braces {}.
The following is an example of using for loop writing.
The code is as follows: |
|
For ($ I = 1; $ I <= 10; $ I ){ Echo "this is the first loop". $ I ." "; } ?>
|
3. switch loop, usually processing compound condition judgment. each sub-condition is part of the case instruction.
In practice, if many similar if commands are used, they can be combined into a switch loop. Syntax:
The code is as follows: |
|
Switch (expr ){ Caseexpr1: Statement1; Break; Caseexpr2: Statement2; Break; Default: StatementN; Break; }
|
The expr condition, usually the variable name.
The exprN after case usually represents the variable value.
The part that meets the condition after the colon.
Note that you must use the break to skip the cycle.
The code is as follows: |
|
Switch (date ("D ")){ Case "Mon ": Echo "today Monday "; Break; Case "Tue ": Echo "Tuesday "; Break; Case "Wed ": Echo "Wednesday "; Break; Case "Thu ": Echo "Thursday "; Break; Case "Fri ": Echo "Today's Friday "; Break; Default: Echo "today's holiday "; Break; } ?> |
Note break;
Do not omit it. default is acceptable.
Obviously, it is very troublesome to use the if loop in the above example.
Of course, in the design, the conditions with the highest probability should be placed at the beginning, and the minimum conditions should be placed at the end, which can increase the execution efficiency of the program.
In the previous example, because the probability of occurrence is the same every day, you do not need to pay attention to the order of conditions.
Php loop control statement
1. While statement
The code is as follows: |
|
While loop is the simplest loop statement in php. Its syntax format is: While (expression ){ Statement; }
|
When the expression value is true, the statement is executed. after the statement is executed, return to the expression for further judgment. The loop does not appear until the expression value is false.
Instance:
The code is as follows: |
|
$ Num = 1; $ Str = "an even number within 10 is :"; While ($ num <= 10 ){ If ($ num % 2 = 0 ){ $ Str. = $ num .""; } $ Num ++; } Echo $ str; ?>
|
2. Do... While statement
The While statement also has a form of representation, Do... While. syntax:
Do {
Statement;
} While (expression );
The difference between the two is: Do... The While statement has more than one loop than the While statement.
When the While expression value is false, the While loop directly jumps out of the current loop, While Do... The While statement first executes the program block and then judges the expression.
3. For statement
The code is as follows: |
|
A For loop is the most complex loop structure in php. Its syntax format is: For (expression1; expression2; expression3 ){ Statement; }
|
Expression1 is used to obtain the value once unconditionally during the first loop.
Expression2 evaluates the value before each loop starts. if the value is true, statement is executed; otherwise, the system jumps out of the loop and continues to execute. Expression3 is executed after each loop.
Instance:
The code is as follows: |
|
$ Num = 1; For ($ I = 1; $ I <= 100; $ I ++ ){ $ Num * = $ I; } Echo $ num; ?>
|
4. Foreach statement
The Foreach loop is introduced in php4.0 and can only be used as an array. In php5, object support is added. The syntax format of this statement is:
Foreach (array_expression as $ value)
Statement;
Or
Foreach (array_expression as $ key => $ value)
Statement;
The Foreach statement traverses the array array_expression. during each loop, the value in the current array is assigned to $ value (or the following table of the array is assigned to $ key and the corresponding array value is assigned to $ value ), meanwhile, the array pointer moves backward until the traversal ends. When the Foreach statement is used, the array pointer is automatically reset, so you do not need to manually set the pointer position. Instance
The code is as follows: |
|
$ Arr = array ("We", "are", "the", "best", "team ","!"); If (is_array ($ arr) = true ){ Foreach ($ arr as $ key => $ value ){ Echo $ key. "=". $ value ." "; } } Else { Echo "this variable is not an array and cannot use the foreach statement "; } ?>
|
Swicth: Let's introduce the usage of php loop control statements. 1. if... else loops include...