--- 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).