Several methods of PHP loop control statements

Source: Internet
Author: User
Tags foreach switch loop

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

Explained as "What to do if something happens".
The syntax is as follows: if (expr) {statement} The expr in which the condition is judged, is usually a condition judged by the logical operational notation.
and statement for the implementation of the conditions of the program, if the program has only one line, you can omit the curly braces {}.
Example: This example omits curly braces.

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

It is particularly noteworthy here that the decision whether equality is = = rather than =,asp programmer may make this error, = is assignment.
Example: The execution section of this example has three lines and cannot omit curly braces.

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

The first two, except if, plus the condition of else, can be explained as "What to do if something happens, how to deal with 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.
Where else is due to only one row of instructions, so no curly braces are added.

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

The third type is recursive if. else loops, usually used in a variety of decision judgments.
It will be several if.. Else take to combine the use of processing.
Look directly at the example below

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

The previous example uses only the two-level if. else loop, to compare A and b two variables.
You actually want to use this recursive if. else loop, please use caution, because too many layers of the loop can make the logic of the design problem, or less dozen curly braces, will cause the program to appear inexplicable problems.
2, for loop on pure 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, which is usually judged by the logical operation sign (logicaloperators).
EXPR3 the part to be executed after the execution of statement to change the condition for the next round of judgment, such as add one. Wait a minute.
and statement for the implementation of the conditions of the program, if the program has only one line, you can omit the curly braces {}.
The following example is an example written in the For loop.

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

3, switch cycle, usually deal with the conditions of the compound judgment, each child condition, is the case instruction part.
If you use many similar if instructions on the implementation, you can synthesize it 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, usually the variable name.
The EXPRN after the case usually represents the value of the variable.
The colon is followed by the part that meets the condition.
Notice that you want to jump off the loop with a break.

The code is as follows Copy Code
<?php
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 "Today's Holiday";
Break
}
?>

What needs attention here is the break;
Don't omit it, default, it's OK.
Obviously, the example above is cumbersome with the IF loop.
Of course, in the design, the most likely to have the highest probability of the first, the least occurrence of the conditions on the last side, you can increase the efficiency of the implementation 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 statement
1, while statement

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

When the value of the expression expression is true, the statement statement executes, and then returns to the expression expression to continue judging after execution is complete. The loop does not jump until the value of the expression is false.
Instance:

The code is as follows Copy Code
<?php
$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 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 program 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 loop structure in PHP, and the syntax format is:
for (Expression1;expression2;expression3) {
Statement
}

Where: Expression1 The value of the first time when the loop is unconditional.
The expression2 is evaluated before each loop starts, and if the value is true, execute statement, otherwise jump out of the loop and proceed down. The 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, the foreach statement
The Foreach loop is introduced by php4.0 and can only be used in arrays. In PHP5, additional support is added to the object. The syntax format of the statement is:
foreach (array_expression as $value)
Statement
Or
Foreach (array_expression as $key => $value)
Statement
The foreach statement traverses the array array_expression, assigns the value in the current array to $value (or assigns the table below to the $key, the corresponding array value to $value), and the array pointer moves backwards, looping back and forth until the traversal ends. When you use a foreach statement, the array pointer is automatically reset, so you do not need to manually set the pointer position. Instance

  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 "The 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.