PHP7 new features foreach Modification example introduces _php instance

Source: Internet
Author: User

one, foreach () loop the array internal pointer no longer works, before PHP7, when the array passes through a foreach iteration, the arrays pointer moves. Starting now, no longer, see the following code ...

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

PHP5 the results of the run will print int (1) INT (2) bool (FALSE)

The results of the PHP7 run print three int (0), which means that the internal pointer to the array does not change.

The results of the previous run print int (1), int (2), and bool (false)

Second, by the value of the loop, foreach is the copy operation of the array

When foreach loops by value (By-value), foreach operates on a copy of the array. In this way, the changes made to the array during the cycle do not affect the cyclic 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 code above unset the second element of the array in the loop, PHP7 still prints three elements: (0 1 2)
Previous versions of PHP would have skipped 1, printing only (0 2).

Third, in accordance with the reference loop, the changes in the array will affect the loop.

If the method is referenced at the time of the loop, the modification of the array will affect the cyclic behavior. However, the PHP7 version optimizes the maintenance of the following locations in many scenarios. For example, append elements to an array at loop time.

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

The appended element in the above code also participates in the loop, so that PHP7 prints "int (0) int (1)", and the old version only prints "int (0)".

Four, the Simple Object Plain (non-traversable) of the loop.

A loop on a simple object, whether it is looping by value or by reference, is the same as the behavior of iterating through the array of references. However, the management of the location will be more accurate.

The behavior of an iterative object (Traversable objects) object is consistent with the previous.

Editor's note: StackOverflow explained above: Traversable object is one of that implements iterator or Iteratoraggregate interface. If an object implements a iterator or Iteratoraggregate interface, it can be called an iterative object.

The above is a small set to introduce the new characteristics of PHP7 modified, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.