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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools