Usage of foreach () in PHP and comparison with while performance

Source: Internet
Author: User
PHP 4 introduces a foreach structure, much like Perl and other languages. It's just aIterating through an array simple method. foreach can only be used in arrays when attempting to use it for otherData type or an uninitializedVariable error is generated. There are two kinds of syntax, and the second is a useful extension that is minor but the first.

foreach (array_expression as $value)    Statementforeach (array_expression as $key = $value)    statement

the first format iterates through the given array of array_expression. in each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).
The second format does the same thing, except that the key name of the current cell is assigned to the variable $key in each loop.
It is possible to traverse objects since PHP 5.
Note:
when foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the Foreach loop.
Note:
unless the array is referenced, foreach operates on a copy of the specified array, not the array itself. The foreach array pointer has some side effects. Unless you reset it, do not rely on the value of the array pointer in the Foreach loop or after the loop.
Since PHP 5, it is easy to modify the elements of an array by adding & before $value. This method assigns a value to a reference instead of copying a value.

<?php$arr = Array (1, 2, 3, 4), foreach ($arr as & $value) {    $value = $value * 2;} $arr is now Array (2, 4, 6, 8)?>

This method is only available if the array being traversed can be referenced (for example, a variable).

<?phpforeach (Array (1, 2, 3, 4) as & $value) {    $value = $value * 2;}? >

foreach is the operation of an array copy (by copying the arrays), while the while is manipulated by moving the internal indicators of the array, which is generally considered to be faster than foreach (because the foreach first copies the array in the beginning of execution). While the while moves the internal indicator directly. ), but the result is just the opposite.
In the loop is the array "read" operation, then the foreach is faster than the while:

foreach ($array as $value) {echo $value;} while (list ($key) = each ($array)) {echo $array [$key];}

In the loop is an array of "write" operations, while the while is faster than foreach:

foreach ($array as $key = + $value) {echo $array [$key] = $value. '...'; } while (list ($key) = each ($array)) {$array [$key] = $array [$key]. '...'; }

Summary: It is generally assumed that foreach involves a value copy, which must be slower than while, but in fact, if the array is only read in the loop, then foreach is very
fast, because PHP uses a copy mechanism of "reference count, copy on write", that is to say, Even if you copy a variable in PHP, the initial form fundamentally says that
is still a reference, and only when the content of the variable changes, does the actual replication occur, and the reason for this is to save memory consumption and also improve the efficiency of
replication. Thus, the efficient read operation of foreach is not difficult to understand. In addition, since foreach is not suitable for handling array writes, then we can draw a knot
theory, in most cases, like the foreach ($array as $key + $value) in the form of array write code should be replaced by the while (list ( $key) =
each ($array)). The speed differences generated by these techniques may not be obvious in small projects, but in large projects like frames, a single request can easily involve a few
hundred thousands of tens of thousands of array loops, and the difference will be magnified significantly.

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.