# PHP Foreach: All You Need to Know

DevFeed: [PHP Foreach: All You Need to Know](<https://devfeed.tech/articles/php-foreach-all-you-need-to-know-121172.md>)

Original publisher: [Read original article](<https://wpshout.com/php-foreach/>)

Author: David Hayes

Published: 2019-10-01T14:49:58Z

Content type: article

Language: en

Sources: [Back-End Development - WPShout](<https://devfeed.tech/sources/back-end-development-wpshout.md>)

Topics: [PHP](<https://devfeed.tech/topics/php.md>), [iteration](<https://devfeed.tech/topics/iteration.md>), [conditionals](<https://devfeed.tech/topics/conditionals.md>)

Tags: [array](<https://devfeed.tech/tags/array.md>), [arrays](<https://devfeed.tech/tags/arrays.md>), [back-end-development](<https://devfeed.tech/tags/back-end-development.md>), [filter](<https://devfeed.tech/tags/filter.md>), [flow](<https://devfeed.tech/tags/flow.md>), [foreach](<https://devfeed.tech/tags/foreach.md>), [php](<https://devfeed.tech/tags/php.md>), [wps](<https://devfeed.tech/tags/wps.md>), [wps-nf](<https://devfeed.tech/tags/wps-nf.md>)

## AI overview

PHP `foreach` iterates through an array or an object's visible properties, making it useful for processing lists and key-value data. The excerpt shows basic iteration, collecting selected values by appending with `$array[]`, accessing keys and values with `as $key => $value`, and traversing arrays of arrays. It distinguishes `continue`, which skips the remaining work for the current item and moves to the next, from `break`, which ends the loop entirely. The article also contrasts `foreach` over an existing collection with `for` loops, which it presents as more suited to counting through a numeric sequence. It notes that classes can customize iteration with `Iterator` or `IteratorAggregate`, but does not cover those further.

## Source excerpt

One of the most-used language constructs in my life with PHP is foreach. Partly, this is because it's just an exquisitely named language construct, and it maps well to how I think. Partly this is because it's kind of the backbone of a webpage: iterate through a set of things, operating on some selectively, and... Read More "PHP Foreach: All You Need to Know