--- id: php-constants title: "PHP Constants" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["define()", "const keyword", "PHP μƒμˆ˜"] 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", "constants"] raw_sources: ["https://www.w3schools.com/php/php_constants.asp"] applied_in: [] github_commit: "" --- # [[PHP Constants]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `define()` and `const` aren't interchangeable despite both creating constants β€” `define()` runs at RUNTIME (so it can live inside functions/conditionals, but never inside a class to define class constants), while `const` runs at COMPILE-TIME (so it must be top-level, but CAN define class constants), making the choice depend entirely on where in the code you need the constant. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Constant** β€” like a variable but immutable once defined; no `$` sign in the name. [S1] - **`define(NAME, value)`** β€” runtime constant creation; always global scope; usable inside functions/conditionals/loops; CANNOT define class constants. [S1] - **`const NAME = value;`** β€” compile-time constant creation; must be top-level (not inside functions/loops/if-else/try-catch); CAN define class constants; case-sensitive. [S1] - **Array constants** β€” both `define()` and `const` support array values. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - define() basic: `define("GREETING", "Welcome to W3Schools.com!"); echo GREETING;`. [S1] - define() inside a function still creates a global constant: `function myTest() { define("GREETING", "Welcome to W3Schools.com!"); } myTest(); echo GREETING; // accessible outside`. [S1] - const basic: `const GREETING = "Welcome to W3Schools.com!"; echo GREETING;`. [S1] - Array constants: `define("CARS", array("Volvo", "BMW", "Toyota")); echo CARS[0]; const ANIMALS = array("Cat", "Dog", "Horse"); echo ANIMALS[1];`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **define()κ³Ό const의 근본적 차이**: define()은 λŸ°νƒ€μž„μ— μ‹€ν–‰λ˜μ–΄ ν•¨μˆ˜/쑰건문 내뢀에 μ„ μ–Έ κ°€λŠ₯ν•˜μ§€λ§Œ 클래슀 μƒμˆ˜ μ •μ˜ λΆˆκ°€, constλŠ” μ»΄νŒŒμΌνƒ€μž„μ— μ²˜λ¦¬λ˜μ–΄ μ΅œμƒμœ„ μŠ€μ½”ν”„μ—μ„œλ§Œ μ„ μ–Έ κ°€λŠ₯ν•˜μ§€λ§Œ 클래슀 μƒμˆ˜ μ •μ˜λŠ” κ°€λŠ₯ν•˜λ‹€λŠ” 점이 λͺ…ν™•νžˆ λŒ€λΉ„λ¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ define()으둜 μ „μ—­ μƒμˆ˜λ₯Ό μ„ μ–Έν•˜κ³  ν•¨μˆ˜ λ°–μ—μ„œ μ ‘κ·Όν•˜λŠ” νŒ¨ν„΄μ΄ define()의 νŠΉμ΄ν•œ μŠ€μ½”ν”„ λ™μž‘μ„ 보여쀀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) define() creates a global constant even when called inside a function (PHP): ```php function myTest() { define("GREETING", "Welcome to W3Schools.com!"); } myTest(); echo GREETING; // accessible here, outside the function ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP Magic Constants]], [[PHP OOP Constants]], [[PHP Variables]] - **μ°Έμ‘° λ§₯락:** μƒμˆ˜ μ •μ˜ 두 κ°€μ§€ 방법 β€” 맀직 μƒμˆ˜(Magic Constants) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP Constants β€” https://www.w3schools.com/php/php_constants.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP Constants" page (Astra wiki-curation, P-Reinforce v3.1 format).