Several methods of PHP loop control statements

Source: Internet
Author: User
Tags switch loop

In php, loop statements generally use while, for, and foreach, and control statements are if swicth. Next I will 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: Copy code
<? Php
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: Copy code
<? Php
If ($ state = 1 ){
Echo "Haha;
Echo "<br> ";
}
?>

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: Copy code
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: Copy code
<? Php
If ($ state = 1 ){
Echo "Haha ";
Echo "<br> ";
} Else {
Echo "Haha ";
Echo "<br> ";
}
?>

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: Copy code
<? Php
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: Copy code
<? Php
For ($ I = 1; $ I <= 10; $ I
){
Echo "This is the first loop". $ I. "<br> ";
}
?>

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: Copy code
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: Copy code
<? Php
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: Copy code
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: Copy code
<? Php
$ 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: Copy code
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: Copy code
<? Php
$ 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: Copy code
<? Php
$ Arr = array ("We", "are", "the", "best", "team ","!");
If (is_array ($ arr) = true ){
Foreach ($ arr as $ key => $ value ){
Echo $ key. "=". $ value. "<br> ";
}
} Else {
Echo "this variable is not an array and cannot use the foreach statement ";
}
?>

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.