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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor
