---
id: php-looping-foreach
title: "PHP Looping Foreach"
category: "Programming_Language"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["foreach loop", "byref", "PHP foreach λ°λ³΅λ¬Έ"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.9
created_at: 2026-07-04
updated_at: 2026-07-04
review_reason: ""
merge_history: []
tags: ["php", "programming", "w3schools", "loops", "foreach", "arrays"]
raw_sources: ["https://www.w3schools.com/php/php_looping_foreach.asp"]
applied_in: []
github_commit: ""
---
# [[PHP Looping Foreach]]
## π― ν μ€ ν΅μ°° (One-line insight)
By default, modifying the loop variable inside `foreach` does NOT affect the original array β but adding a single `&` (`foreach ($colors as &$x)`) switches to by-reference assignment, meaning changes to `$x` inside the loop DO mutate `$colors` directly, a one-character difference with a completely different mutation contract. [S1]
## π§ ν΅μ¬ κ°λ
(Core concepts)
- **`foreach ($array as $value)`** β loops over indexed array values. [S1]
- **`foreach ($array as $key => $value)`** β loops over associative arrays, exposing both key and value. [S1]
- **Works on objects too** β iterates over public properties (`$name => $value`). [S1]
- **By-value default** β modifying the loop variable does not change the source array. [S1]
- **By-reference (`&$x`)** β modifying the loop variable DOES change the source array. [S1]
- **`break`** / **`continue`** β same semantics as other loops. [S1]
- **Alternative syntax** β `foreach (...) : ... endforeach;`. [S1]
## π μΈλΆ λ΄μ© (Details)
- Indexed array: `$colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) { echo "$value
"; }`. [S1]
- Associative array: `$members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); foreach ($members as $key => $value) { echo "$key : $value
"; }`. [S1]
- Object properties: `foreach ($myCar as $x => $y) { echo "$x: $y
"; }`. [S1]
- By-value (no mutation): `foreach ($colors as $x) { if ($x == "blue") $x = "pink"; } // $colors unchanged`. [S1]
- By-reference (mutates original): `foreach ($colors as &$x) { if ($x == "blue") $x = "pink"; } // $colors now has "pink" instead of "blue"`. [S1]
## βοΈ λͺ¨μ λ° μ
λ°μ΄νΈ (Contradictions & updates)
- **μ°Έμ‘° μ λ¬μ μμΈμ λμ**: κΈ°λ³Έμ μΌλ‘ λ°λ³΅ λ³μ μμ μ μλ³Έ λ°°μ΄μ μν₯μ μ£Όμ§ μμ§λ§, &λ₯Ό λΆμ΄λ©΄ μλ³Έμ΄ μ€μ λ‘ λ³κ²½λλ€λ μ μ΄ λ μμ λ‘ λͺ
νν λλΉλ¨. [S1]
## π οΈ μ μ© μ¬λ‘ (Applied in summary)
νμ¬ λ°κ²¬λ μ€μ μ μ© μ¬λ‘κ° μμ΅λλ€ β λ°°μ΄ κ°μ μννλ©° νΉμ 쑰건μ λ°λΌ μλ³Έμ μ§μ μμ ν΄μΌ ν λ &$x μ°Έμ‘° μ λ¬μ΄ μ¬μ©λλ€. [S1]
## π» μ½λ ν¨ν΄ (Code patterns)
By-reference foreach mutates the original array (PHP):
```php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as &$x) {
if ($x == "blue") $x = "pink";
}
var_dump($colors); // "blue" is now "pink" in the original array
```
## β
κ²μ¦ μν λ° μ λ’°λ
- **μν:** draft
- **κ²μ¦ λ¨κ³:** conceptual
- **μΆμ² μ λ’°λ:** B (W3Schools β widely used educational reference, not a primary standards body)
- **μ λ’° μ μ:** 0.90
- **μ€λ³΅ κ²μ¬ κ²°κ³Ό:** μ κ· μμ± (New discovery)
## π μ§μ κ·Έλν (Knowledge Graph)
- **μμ/루νΈ:** [[PHP Tutorial]]
- **κ΄λ ¨ κ°λ
:** [[PHP Looping For]], [[PHP Arrays]], [[PHP OOP Classes Objects]]
- **μ°Έμ‘° λ§₯λ½:** λ°°μ΄/κ°μ²΄ μν μ μ© λ°λ³΅λ¬Έ β break/continue μ±ν°λ‘ μ΄μ΄μ§.
## π μΆμ² (Sources)
- [S1] W3Schools β PHP foreach Loop β https://www.w3schools.com/php/php_looping_foreach.asp
## π λ³κ²½ μ΄λ ₯ (Change history)
- 2026-07-04: Initial draft synthesized from the W3Schools "PHP foreach Loop" page (Astra wiki-curation, P-Reinforce v3.1 format).