PHP loop control Statements several methods _php tutorial

Source: Internet
Author: User
In PHP, the loop statement is generally used While,for,foreach and control statements is if swicth these, let me introduce you to the PHP loop control statement usage.

1. If: Else loop there are three kinds of structures the first is to use the IF condition as a mere judgment.

Explain how to deal with something if it happens.
The syntax is as follows: if (expr) {statement} where expr is a condition of judgment, it is usually the condition that is judged by the logical operation symbol.
And statement is a qualifying execution part of the program, if the program has only one row, you can omit the curly brace {}.
Example: This example omits curly braces.

The code is as follows Copy Code
if ($state==1)
echo "haha";
?>

It is particularly important to note that determining whether equality is = = rather than =,asp programmers can often make this error, = is an assignment.
Example: The execution section of this example has three rows and cannot omit curly braces.

The code is as follows Copy Code
if ($state==1) {
echo "haha;
Echo
";
}
?>

The first two is the addition of the IF condition, which can be interpreted as "what to do if something happens or how to resolve it".
The syntax is as follows:

The code is as follows Copy Code
if (expr) {
Statement1
}else{
Statement2
}

Example: The above example is modified to a more complete processing.
The else in which there is only one line of instructions, so do not add braces.

The code is as follows Copy Code
if ($state==1) {
echo "haha";
Echo
";
}else{
echo "hehe";
Echo
";
}
?>

The third type is recursive if. else loops, often used in a variety of decision-making judgments.
It will be a few if. Else to combine the use of processing.
Look directly at the following example

The code is as follows Copy Code
if ($a>$b) {
echo "A is larger than B";
}elseif ($a==$b) {
echo "a equals B";
}else{
echo "A is smaller than B";
}
?>

The above example uses only two layers of if. else loop, which is used to compare the two variables of a and B.
This recursive if is actually to be used. else loop, please be careful to use, because too many layers of the loop is easy to make the logic of the design problems, or less curly braces, etc., will cause the program to appear inexplicable problem.
2, for the loop is only one, no change, its syntax is as follows
for (EXPR1;EXPR2;EXPR3) {statement}
Where the EXPR1 is the initial value of the condition.
EXPR2 is a condition for judging, usually using the logic operation symbol (logicaloperators) when judging the condition.
EXPR3 the part to be executed after the execution of the statement, to change the condition for the next cycle judgment, such as add one. Wait a minute.
And statement is a qualifying execution part of the program, if the program has only one row, you can omit the curly brace {}.
The following example is an example written with a for loop.

The code is as follows Copy Code
for ($i=1;$i<=10;$i
){
echo "This is the first". $i. " Secondary cycle
";
}
?>

3, switch loop, usually deal with the conditional judgment of the compound, each sub-condition, is the case instruction part.
In practice, if you use many similar if directives, you can combine them into a switch loop. The syntax is as follows

The code is as follows Copy Code
Switch (expr) {
CASEEXPR1:
Statement1;
Break
CASEEXPR2:
Statement2;
Break
Default
STATEMENTN;
Break
}

The expr condition, which is usually the variable name.
The exprn after a case usually represents the value of the variable.
The colon is followed by the part that meets the criteria to be executed.
Note to use break to jump off the loop.

The code is as follows Copy Code
Switch (date ("D")) {
Case "Mon":
echo "Today Monday";
Break
Case "Tue":
echo "Today Tuesday";
Break
Case "Wed":
echo "Today Wednesday";
Break
Case "Thu":
echo "Today Thursday";
Break
Case "Fri":
echo "Today Friday";
Break
Default
echo "Holiday Today";
Break
}
?>

The point to note here is break;
Don't miss out, default, omitting is possible.
Obviously, the above example is very troublesome with the IF loop.
Of course, in the design, the most likely to be the highest probability of the conditions at the front, the least occurrence of the conditions on the last side, you can increase the efficiency of the execution of the program.
The previous example has the same probability of appearing every day, so don't pay attention to the order of the conditions.

PHP Loop Control Statements
1. While statement

The code is as follows Copy Code
The while loop is the simplest of the looping statements in PHP, and his syntax format is:
while (expression) {
Statement
}

When the value of expression expressions is true, the statement statement is executed, the execution finishes, and then returns to expression expressions to continue the judgment. The loop is not popped out until the value of the expression is false.
Instance:

The code is as follows Copy Code
$num = 1;
$str = "The 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 is:
do{
Statement
}while (expression);
The difference between the two is: do ... The while statement loops more than the while statement.
When the value of the while expression is false, the while loop jumps directly out of the current loop, and do ... The while statement executes the block first, and then the expression is judged.
3. For statement

The code is as follows Copy Code
The For loop is the most complex looping structure in PHP, and its syntax is:
for (Expression1;expression2;expression3) {
Statement
}

Where: Expression1 The first time the loop is unconditionally take the value.
The expression2 is evaluated before each cycle starts, and if the value is true, the statement is executed, otherwise it jumps out of the loop and continues down. The Expression3 is executed after each loop.
Instance:

The code is as follows Copy Code
$num = 1;
for ($i =1; $i <=100; $i + +) {
$num *= $i;
}
Echo $num;
?>

4. foreach statement
The Foreach loop is introduced by php4.0 and can only be used for arrays. In PHP5, support for objects is added. The syntax format for this statement is:
foreach (array_expression as $value)
Statement
Or
Foreach (array_expression as $key = $value)
Statement
The foreach statement iterates through the array array_expression, assigning the values in the current array to $value (or assigning the array's following table to $key, the corresponding array value to $value) each time the loop is iterated, while the array pointer moves backwards so that it loops back and forth until the end of the traversal. When a 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
$arr =array ("We", "is", "the", "Best", "Team", "!");
if (Is_array ($arr) = = True) {
foreach ($arr as $key = = $value) {
echo $key. " = ". $value."
”;
}
}else{
echo "The variable is not an array and cannot use a foreach statement";
}
?>

http://www.bkjia.com/PHPjc/632683.html www.bkjia.com true http://www.bkjia.com/PHPjc/632683.html techarticle in PHP, the loop statement is generally used While,for,foreach and control statements is if swicth these, let me introduce you to the PHP loop control statement usage. 1. If: else Loop has ...

  • 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.