--- id: php-variables-scope title: "PHP Variables Scope" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["PHP λ³€μˆ˜ λ²”μœ„", "global keyword", "static keyword"] 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", "scope"] raw_sources: ["https://www.w3schools.com/php/php_variables_scope.asp"] applied_in: [] github_commit: "" --- # [[PHP Variables Scope]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `static` variables break the normal "destroyed on function exit" rule β€” a static local variable retains its value between separate calls to the same function, meaning `static $x = 0; $x++;` accumulates across calls even though it's still only accessible inside that one function (local scope + persistent state, a combination unique to static). [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Global scope** β€” a variable declared outside any function; NOT accessible inside functions by default. [S1] - **Local scope** β€” a variable declared inside a function; created on call, destroyed on function exit. [S1] - **Static scope** β€” `static $x = 0;` preserves the variable's value across multiple calls to the same function, while remaining local (inaccessible outside). [S1] - **`global` keyword** β€” inside a function, declares that a name refers to the global variable rather than creating a new local one. [S1] - **`$GLOBALS` superglobal** β€” an array holding all global variables, accessible and mutable from inside any function without the `global` keyword. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Global scope inaccessible inside function: `$x = 5; function myTest() { echo "Variable x inside function is: $x"; } // does not print 5`. [S1] - Local scope: `function myTest() { $x = 5; echo "Variable x inside function is: $x"; } // $x undefined outside`. [S1] - Static persists across calls: `function myTest() { static $x = 0; echo $x; $x++; } myTest(); myTest(); myTest(); // 0 1 2`. [S1] - global keyword: `$x = 5; $y = 10; function myTest() { global $x, $y; $y = $x + $y; } myTest(); echo $y; // 15`. [S1] - $GLOBALS equivalent: `function myTest() { $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y']; } myTest(); echo $y; // 15`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **static의 μ˜ˆμ™Έμ  지속성**: ν•¨μˆ˜ μ’…λ£Œ μ‹œ 일반 μ§€μ—­λ³€μˆ˜λŠ” μ‚­μ œλ˜μ§€λ§Œ static λ³€μˆ˜λŠ” 값을 μœ μ§€ν•˜λ©°, μ—¬μ „νžˆ ν•¨μˆ˜ μ§€μ—­ λ²”μœ„μ— ν•œμ •λœλ‹€λŠ” 이쀑적 νŠΉμ„±μ΄ λͺ…μ‹œλ¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” 호좜 횟수λ₯Ό μ„ΈλŠ” μΉ΄μš΄ν„°(static $x)κ°€ 정적 μŠ€μ½”ν”„ ν™œμš©μ˜ λŒ€ν‘œ 사둀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Static variable persisting across function calls (PHP): ```php function myTest() { static $x = 0; echo $x; $x++; } myTest(); // 0 myTest(); // 1 myTest(); // 2 ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP Variables]], [[PHP Superglobals Globals]], [[PHP Echo Print]] - **μ°Έμ‘° λ§₯락:** λ³€μˆ˜ μŠ€μ½”ν”„ κ·œμΉ™ β€” $GLOBALSλŠ” Superglobals μ±•ν„°μ—μ„œ 심화. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP Variable Scope β€” https://www.w3schools.com/php/php_variables_scope.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP Variable Scope" page (Astra wiki-curation, P-Reinforce v3.1 format).