Differences between php loop statements for () and foreach ()
Source: Internet
Author: User
Next, I will use two instances to introduce the differences between foreach and for statements in php. if you need them, refer to the for loop, which 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:
The code is as follows:
For ($ I = 1; $ I <= 10; $ I ++ ){
Print $ I;
}
For ($ I = 1; $ I ++ ){
If ($ I> 10 ){
Break;
}
Print $ I;
}
$ I = 1;
For (;;){
If ($ I> 10 ){
Break;
}
Print $ I;
$ I ++;
}
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. Foreach
PHP 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_r_r as $ value) statement foreach (array_expression_r_r as $ key => $ value) statement
The first format traverses the given array_expression_r_r 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:
The code is as follows:
$ Arr = array ("one", "two", "three ");
Reset ($ arr );
While (list (, $ value) = each ($ arr )){
Echo "Value: $ value
\ N ";
}
Foreach ($ arr as $ value ){
Echo "Value: $ value
\ N ";
}
?>
The following code has the same functions:
The code is as follows:
Reset ($ arr );
While (list ($ key, $ value) = each ($ arr )){
Echo "Key: $ key; Value: $ value
\ N ";
}
Foreach ($ arr as $ key => $ value ){
Echo "Key: $ key; Value: $ value
\ N ";
}
?>
More examples of demo usage:
The code is as follows:
$ A = array (1, 2, 3, 17 );
Foreach ($ a as $ v ){
Print "Current value of \ $ a: $ v. \ n ";
}
$ A = array (1, 2, 3, 17 );
$ I = 0;
Foreach ($ a as $ v ){
Print "\ $ a [$ I] => $ v. \ n ";
$ I ++;
}
$ A = array (
"One" => 1,
"Two" => 2,
"Three" => 3,
"Seventeen" => 17
);
Foreach ($ a as $ k => $ v ){
Print "\ $ a [$ k] => $ v. \ n ";
}
$ A [0] [0] = "";
$ A [0] [1] = "B ";
$ A [1] [0] = "y ";
$ A [1] [1] = "z ";
Foreach ($ a as $ v1 ){
Foreach ($ v1 as $ v2 ){
Print "$ v2 \ n ";
}
}
Foreach (array (1, 2, 3, 4, 5) as $ v ){
Print "$ v \ n ";
}
?>
The code is as follows:
// Foreach
$ Tar = array (
1 => 'East ',
2 => 'West ',
3 => 'south ',
4 => 'North ',
5 => 'Southeast ',
6 => 'southwest ',
7 => 'Northeast region ',
8 => 'Northwest ',
9 => 'North and South ',
10 => 'thes ',
);
$ TM = 'West ';
Foreach ($ tar as $ v => $ vv)
{
If ($ vv = $ TM)
{
Echo $ vv. '-'. $ v .'
';
Break;
}
// Echo $ vv;
}
// West-2
//
The code is as follows:
Echo'
';
For ($ I = 1; $ I <= count ($ tar); $ I ++)
{
If ($ tar [$ I] ==$ TM)
{
Echo $ tar [$ I]. '-'. $ I .'
';
Break;
}
}
// West-2
Conclusion: foreach and for results are exactly the same, but in terms of efficiency, foreach is better than for. The homepage for needs to know the array length and then use $ I ++ for operations, page foreach does not need to know the length of the array to automatically detect and enter the key, and value.
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.