Home > php教程 > PHP源码 > body text

PHP7 new feature foreach modification usage example

PHP中文网
Release: 2016-07-06 13:34:23
Original
1902 people have browsed it

PHP7’s new feature foreach has slightly changed from the previous version. So what changes have been made in PHP7’s new feature foreach? Let’s take a look at the usage examples of PHP7’s new feature foreach. I hope this article can help everyone.

<script>ec(2);</script>
Copy after login

1. The foreach() loop no longer works on the internal pointer of the array. Before PHP7, when the array was iterated through foreach, the array pointer would move. From now on, this is no longer the case, see the code below. .

$array = [0, 1, 2];
foreach ($array as &$val) 
{
    var_dump(current($array));
}
Copy after login

The result of PHP5 operation will print int(1) int(2) bool(false)
The result of PHP7 operation will print int(0) three times, which means that the array The internal pointer is not changed.
The results of the previous operation will print int(1), int(2) and bool(false)

2. When looping according to the value, foreach is a copy operation of the array

When foreach loops by value (by-value), foreach operates on a copy of the array. In this way, modifications to the array during the loop will not affect the loop behavior.

$array = [0, 1, 2];
$ref =& $array; // Necessary to trigger the old behavior
foreach ($array as $val) {
    var_dump($val);
    unset($array[1]);
}
Copy after login

Although the above code unsets the second element of the array in the loop, PHP7 will still print out the three elements: (0 1 2)
Old version of PHP 1 will be skipped and only (0 2) will be printed.

3. When looping by reference, modifications to the array will affect the loop.

If you use references when looping, modifications to the array will affect the loop behavior. However, the PHP7 version optimizes the maintenance of locations under many scenarios. For example, appending elements to an array while looping.

$array = [0];
foreach ($array as &$val) {
    var_dump($val);
    $array[1] = 1;
}
Copy after login

The appended elements in the above code will also participate in the loop, so PHP7 will print "int(0) int(1)", and the old version will only print "int(0)".

4. Looping over simple objects plain (non-Traversable).

Looping over simple objects, whether looping by value or looping by reference, behaves the same as looping by reference in an array. However, location management will be more precise.

5. The object behavior for Traversable objects is the same as before.

Editor's note: stackoverflow's explanation above: Traversable object is one that implements Iterator or IteratorAggregate interface. If an object implements the iterator or IteratorAggregate interface, it can be called an iterator object.

The above is the content of the usage example of PHP7’s new feature foreach modification. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template