Files
2nd/10_Wiki/Dev/Topic_PHP/PHP_Looping_Foreach.md
T
Antigravity Agent 1cfd3bbb56 docs(10_Wiki): 위키 구조 정리 — 언어 튜토리얼 카테고리 폴더 제거 + 신규 자산 동기화
Topic_CSS/Topic_HTML/Topic_JavaScript/Topic_Prompt/Topic_Comfyui 등 기존 카테고리 폴더를 정리하고,
Topic_Graphic/Dev 등 신규 산출물과 Topics 내부 세션/메모리 기록을 동기화.
2026-07-05 00:10:59 +09:00

3.8 KiB

id, title, category, status, verification_status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, created_at, updated_at, review_reason, merge_history, tags, raw_sources, applied_in, github_commit
id title category status verification_status canonical_id aliases duplicate_of source_trust_level confidence_score created_at updated_at review_reason merge_history tags raw_sources applied_in github_commit
php-looping-foreach PHP Looping Foreach Programming_Language draft conceptual
foreach loop
byref
PHP foreach 반복문
B 0.9 2026-07-04 2026-07-04
php
programming
w3schools
loops
foreach
arrays
https://www.w3schools.com/php/php_looping_foreach.asp

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 syntaxforeach (...) : ... endforeach;. [S1]

📖 세부 내용 (Details)

  • Indexed array: $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) { echo "$value <br>"; }. [S1]
  • Associative array: $members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); foreach ($members as $key => $value) { echo "$key : $value <br>"; }. [S1]
  • Object properties: foreach ($myCar as $x => $y) { echo "$x: $y <br>"; }. [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):

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

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-07-04: Initial draft synthesized from the W3Schools "PHP foreach Loop" page (Astra wiki-curation, P-Reinforce v3.1 format).