Home  >  Article  >  Backend Development  >  Usage of foreach() in php and performance comparison with while

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

伊谢尔伦
伊谢尔伦Original
2017-06-23 11:42:401058browse

PHP 4 introduced the foreach construct, much like Perl and other languages. This is just a convenient way of traversing an array. foreach can only be used on arrays, and an error will occur when trying to use it on other data types or an uninitialized variable . There are two syntaxes, the second being a less important but useful extension of the first.

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 one step (so the next unit will be obtained in the next loop).
The second format does the same thing, except that the key name of the current unit will also be assigned to the variable $key in each loop.
Since PHP 5, it is also possible to traverse objects.
Note:
When foreach starts executing, the pointer inside the array will automatically point to the first unit. This means there is no 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. foreach has some side effects on array pointers. Do not rely on the value of an array pointer during or after a foreach loop unless it is reset.
Since PHP 5, it is easy to modify the elements of an array by adding & before $value. This method assigns by reference rather than copying a value.

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

foreach operates on a copy of the array (by copying the array), while while operates by moving the internal index of the array. Under general logic, it is believed that while should be faster than foreach (because foreach first starts executing Copy the array into it, and while directly moves the internal pointer), but the result is just the opposite.
In the loop, the array "read" operation is performed, so foreach is faster than while:

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

In the loop, the array "write" operation is performed, then 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 believed that foreach involves value copying and will be slower than while. However, in fact, if you only read the array in a loop, then foreach is very
fast. This is because PHP The copy mechanism adopted is "reference counting, copy-on-write", that is to say, even if a variable is copied in PHP, the original form is still fundamentally in the form of a reference, and only when the content of the variable changes When, real copying will occur. The reason for doing this is to save memory consumption, and at the same time, it also improves the efficiency of
copying. From this point of view, the efficient read operation of foreach is not difficult to understand. In addition, since foreach is not suitable for processing array write operations, we can draw the conclusion that in most cases, the code for array write operations in the form of foreach ($array as $key => $value) is Should be replaced by while (list($key) =
each($array)). The speed difference produced by these techniques may not be obvious in small projects, but in large projects like frameworks, where a single request often involves hundreds, thousands, or tens of millions of array loop operations, the difference will be obvious. enlarge.

The above is the detailed content of Usage of foreach() in php and performance comparison with while. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn