Home >php教程 >PHP源码 >PHP7 new feature foreach modification usage example

PHP7 new feature foreach modification usage example

PHP中文网
PHP中文网Original
2016-07-06 13:34:231932browse

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>

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));
}

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]);
}

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;
}

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)!


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