Php loop control statement (for, foreach while do while)

Source: Internet
Author: User
Tags php tutorial reset

Foreach

Php Tutorial 4 (not php 3) includes the foreach structure, which is similar to perl and other languages. This is just a simple way to traverse arrays. Foreach can only be used for arrays. An error occurs when you try to use it for another data type or an uninitialized variable. There are two types of syntax. The second type is secondary, but it is the first type of useful extension.


Foreach (array_expression as $ value) statement
Foreach (array_expression as $ key => $ value) statement


 

 

The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).

In the second format, only the key values of the current unit are assigned to the variable $ key in each loop.


Note: When foreach starts to execute, the pointer inside the array automatically points to the first unit. This means that you do not need to call reset () before the foreach loop ().

 

Note: foreach operates on a copy of the specified array, rather than the array itself. Therefore, even if each () is constructed, the original array pointer is not changed, and the value of the array unit is not affected.

 

Note: foreach does not support "@" to prohibit error messages.


You may notice that the following code functions are identical:


<? Php
$ Arr = array ("one", "two", "three ");
Reset ($ arr );
While (list (, $ value) = each ($ arr )){
Echo "value: $ value <br> n ";
}

Foreach ($ arr as $ value ){
Echo "value: $ value <br> n ";
}
?>


The following code has the same functions:

<? Php
Reset ($ arr );
While (list ($ key, $ value) = each ($ arr )){
Echo "key: $ key; value: $ value <br> n ";
}

Foreach ($ arr as $ key => $ value ){
Echo "key: $ key; value: $ value <br> n ";
}
?>

 

More examples of demo usage:

 

<? Php
/* Foreach example 1: value only */

$ A = array (1, 2, 3, 17 );

Foreach ($ a as $ v ){
Print "current value of $ a: $ v. n ";
}

/* Foreach example 2: value (with key printed for authentication )*/

$ A = array (1, 2, 3, 17 );

$ I = 0;/* for each strative purposes only */

Foreach ($ a as $ v ){
Print "$ a [$ I] => $ v. n ";
$ I ++;
}

/* Foreach example 3: key and value */

$ A = array (
"One" => 1,
"Two" => 2,
"Three" => 3,
"Seventeen" => 17
);

Foreach ($ a as $ k => $ v ){
Print "$ a [$ k] => $ v. n ";
}

/* Foreach example 4: multi-dimen1_arrays */

$ A [0] [0] = "";
$ A [0] [1] = "B ";
$ A [1] [0] = "y ";
$ A [1] [1] = "z ";

Foreach ($ a as $ v1 ){
Foreach ($ v1 as $ v2 ){
Print "$ v2n ";
   }
}

/* Foreach example 5: dynamic arrays */

Foreach (array (1, 2, 3, 4, 5) as $ v ){
Print "$ vn ";
}
?>


For
A for loop is the most complex loop structure in php. Its behavior is similar to that of c language. The syntax of the for loop is:

 

For (expr1; expr2; expr3) statement


 

 

The first expression (expr1) is unconditionally evaluated once before the loop starts.

Expr2 is evaluated before each cycle starts. If the value is true, the loop continues and the nested loop statement is executed. If the value is false, the loop is terminated.

Expr3 is evaluated (executed) after each loop ).

Each expression can be empty. If expr2 is null, it means an infinite loop (like c, php considers its value true ). This may not be as useful as you think, because you often want to use the break statement to end the loop instead of using the true value of the for expression.

Consider the following example. They all show numbers 1 to 10:

 

<? Php
/* Example 1 */

For ($ I = 1; $ I <= 10; $ I ++ ){
Print $ I;
}

/* Example 2 */

For ($ I = 1; $ I ++ ){
If ($ I> 10 ){
Break;
   }
Print $ I;
}

/* Example 3 */

$ I = 1;
For (;;){
If ($ I> 10 ){
Break;
   }
Print $ I;
$ I ++;
}

/* Example 4 */

For ($ I = 1; $ I <= 10; print $ I, $ I ++ );
?>

Of course, the first example looks the most normal (or the fourth one), but you may find it convenient to use an empty expression in a for loop in many cases.

Php also supports the replacement syntax of the for loop with colons.

 

For (expr1; expr2; expr3): statement;...; endfor;


 

 

Other languages have foreach statements to traverse arrays or scattered lists. php can also (see foreach ). In php 3, you can combine the list () and each () functions to use a while loop to achieve the same effect. For examples, see the documentation for these functions.


Do... while
Do... while and while loops are very similar. The difference is that the expression value is checked at the end of each loop rather than at the start. The main difference from a regular while loop is do .. the while loop statement is always executed once (the true value of the expression is checked after the end of each loop), but it is not necessarily in the regular while loop (the true value of the expression is checked when the loop starts, if it is set to false at the beginning, the entire loop is terminated immediately ).

Do... the while loop has only one syntax:


<? Php
$ I = 0;
Do {
Print $ I;
} While ($ I> 0 );
?>

 

The above Loop runs exactly once, because after the first loop, when the true value of the expression is checked, its value is false ($ I is not greater than 0), leading to loop termination.

Senior c-language users may be familiar with another different do .. while loop usage, put the statement in do .. in while (0), the break statement is used inside the loop to end the execution loop. The following code snippet demonstrates this method:

 

<? Php
Do {
If ($ I <5 ){
Print "I is not big enough ";
Break;
   }
$ I * = $ factor;
If ($ I <$ minimum_limit ){
Break;
   }
Print "I is OK ";

/* Process I */

} While (0 );
?>

Do not worry if you cannot understand it immediately. You can write powerful code even if you don't need this "feature.


While
While loop is the simplest loop type in php. It performs the same way as while in c. The basic format of the while statement is:

 

While (expr) statement


 

 

The while statement indicates that php repeats the nested loop statement as long as the while expression value is true. The value of the expression is checked when the loop starts. Therefore, even if the value changes in the loop statement, the statement will not stop until the loop ends. Sometimes, if the value of the while expression is false at the beginning, the loop statement will not be executed once.

Like if statements, you can enclose a statement group in curly brackets in a while loop or use an alternative syntax:

 

While (expr): statement... endwhile;


 

 

The following two examples show numbers 1 to 10 in the same way:

 

<? Php
/* Example 1 */

$ I = 1;
While ($ I <= 10 ){
Print $ I ++;/* the printed value wocould be
$ I before the increment
(Post-increment )*/
}

/* Example 2 */

$ I = 1;
While ($ I <= 10 ):
Print $ I;
$ I ++;
Endwhile;
?>

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.