Home > Backend Development > PHP Tutorial > How Does PHP's `foreach` Loop Actually Work, and How Has Its Implementation Changed Between PHP 5 and PHP 7?

How Does PHP's `foreach` Loop Actually Work, and How Has Its Implementation Changed Between PHP 5 and PHP 7?

Mary-Kate Olsen
Release: 2024-12-23 22:20:18
Original
385 people have browsed it

How Does PHP's `foreach` Loop Actually Work, and How Has Its Implementation Changed Between PHP 5 and PHP 7?

How foreach actually works in PHP?

Introduction:
foreach is used extensively in PHP for looping through arrays, objects, and other data structures. Understanding its inner workings is crucial to avoid unexpected behavior.

Three Types of foreach Iterables:
foreach supports three types of iterables:

  • Arrays
  • Normal objects
  • Traversable objects

Traversable Object Iteration:
For Traversable objects, foreach acts as syntactical sugar for:

foreach ($it as $k => $v) {}

translates to:

if ($it instanceof IteratorAggregate) {
    $it = $it->getIterator();
}
for ($it->rewind(); $it->valid(); $it->next()) {
    $v = $it->current();
    $k = $it->key();
    // ...
}
Copy after login

Array and Object Iteration:

Ordered Dictionaries:
PHP arrays and objects are essentially ordered dictionaries that maintain the order of insertion. Iteration follows this order.

Conversion of Objects:
Packed object representations are converted to dictionaries during iteration. Plain-object iteration thus becomes similar to array iteration.

Internal Array Pointer (IAP):
Arrays in PHP 5 have an internal array pointer (IAP) that supports modifications and skipping over removed elements.

HashPointer:
foreach in PHP 5 uses a HashPointer to back up the IAP position and hash before running the loop body. It restores it afterward if the element still exists; otherwise, it uses the current IAP position.

Array Duplication:
foreach in PHP 5 duplicates arrays that are not references (is_ref=0) and have a refcount greater than 1.

PHP 5 Examples:

  • In test cases 1 and 2, arrays are not duplicated because they have refcount=1 and are passed by value.
  • In test case 3, the IAP is set to the end of the array after the loop due to element removal.
  • In test cases 4 and 5, arrays are duplicated because the by-ref functions (each, reset) receive a reference to a duplicated array.

PHP 7 Changes:

Hashtable Iterators:
PHP 7 uses external hashtable iterators, registered in the array. Each iterator is advanced if the corresponding element is removed.

No IAP Usage by foreach:
foreach no longer uses the IAP, eliminating the need for duplication. By-value array iteration always uses the original elements.

Array Duplication:
Arrays are no longer duplicated during by-value iteration, regardless of their refcount or reference status.

PHP 7 Examples:

  • In PHP 7, test cases 1 and 2 have the same output as in PHP 5 because the arrays are not duplicated.
  • In PHP 7, test case 3 produces different output because the loop no longer affects the IAP.
  • In PHP 7, test cases 4 and 5 behave similarly because each and reset still duplicate the array, while foreach uses the original array.

Additional PHP 7 Enhancements:

  • Outer loops in nested iterations are no longer aborted when elements are removed in inner loops.
  • The "arrow in time" problem is resolved.

The above is the detailed content of How Does PHP's `foreach` Loop Actually Work, and How Has Its Implementation Changed Between PHP 5 and PHP 7?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template