PHP7 new feature foreach modification usage example
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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values swapped. $original_array=[

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.
