--- id: php-match title: "PHP Match" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["match expression", "PHP match"] 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", "match", "php8"] raw_sources: ["https://www.w3schools.com/php/php_match.asp"] applied_in: [] github_commit: "" --- # [[PHP Match]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) `match` (PHP 8.0+) uses STRICT comparison (`===`) internally while `switch` uses loose comparison (`==`) โ€” this means `match` won't accidentally match a string `"1"` against an int `1` the way `switch` might, making `match` both safer and more predictable, on top of returning a value and auto-breaking after any match. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **`match`** (PHP 8.0+) โ€” evaluates an expression against alternatives using strict (`===`) comparison, returns a value. [S1] - **Four key differences from switch** โ€” more readable syntax; returns a value (switch doesn't); auto-breaks after a match (switch needs explicit `break`); strict comparison (switch uses loose `==`). [S1] - **Syntax** โ€” `$result = match($expr) { cond1 => val1, cond2, cond3 => val2, default => defaultVal };`. [S1] - **Multiple conditions per arm** โ€” comma-separated conditions sharing one result. [S1] - **`UnhandledMatchError`** โ€” thrown if no condition matches AND there's no `default` arm. [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - Basic match (equivalent to the switch example): `$favcolor = "red"; $text = match($favcolor) { "red" => "Your favorite color is red!", "blue" => "...", "green" => "...", default => "..." }; echo $text;`. [S1] - Multiple values per arm: `$text = match($d) { 1, 2, 3, 4, 5 => "The week feels so long!", 6, 0 => "Weekends are best!", default => "Invalid day" };`. [S1] - No match, no default โ†’ exception: `$favcolor = "pink"; try { $text = match($favcolor) { "red" => ..., "blue" => ..., "green" => ... }; } catch (\UnhandledMatchError $e) { var_dump($e); }`. [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **match์™€ switch์˜ ๊ทผ๋ณธ์  ์ฐจ์ด 4๊ฐ€์ง€**: ๊ฐ’ ๋ฐ˜ํ™˜ ์—ฌ๋ถ€, ์ž๋™ break ์—ฌ๋ถ€, ์—„๊ฒฉ ๋น„๊ต(===) vs ๋А์Šจํ•œ ๋น„๊ต(==)๊ฐ€ ๋ช…ํ™•ํžˆ ๊ตฌ๋ถ„๋จ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) ํ˜„์žฌ ๋ฐœ๊ฒฌ๋œ ์‹ค์ œ ์ ์šฉ ์‚ฌ๋ก€๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค โ€” ๊ฐ’์ด ์—†์„ ๋•Œ UnhandledMatchError๋ฅผ try-catch๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ํŒจํ„ด์ด ๊ฒฌ๊ณ ํ•œ ๋งค์น˜ ๋กœ์ง์˜ ์‹ค์ „ ์˜ˆ์‹œ๋‹ค. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) Match expression with strict comparison and value return (PHP 8+): ```php $favcolor = "red"; $text = match($favcolor) { "red" => "Your favorite color is red!", "blue" => "Your favorite color is blue!", "green" => "Your favorite color is green!", default => "Your favorite color is neither red, blue, nor green!", }; echo $text; ``` ## โœ… ๊ฒ€์ฆ ์ƒํƒœ ๋ฐ ์‹ ๋ขฐ๋„ - **์ƒํƒœ:** draft - **๊ฒ€์ฆ ๋‹จ๊ณ„:** conceptual - **์ถœ์ฒ˜ ์‹ ๋ขฐ๋„:** B (W3Schools โ€” widely used educational reference, not a primary standards body) - **์‹ ๋ขฐ ์ ์ˆ˜:** 0.90 - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ:** ์‹ ๊ทœ ์ƒ์„ฑ (New discovery) ## ๐Ÿ”— ์ง€์‹ ๊ทธ๋ž˜ํ”„ (Knowledge Graph) - **์ƒ์œ„/๋ฃจํŠธ:** [[PHP Tutorial]] - **๊ด€๋ จ ๊ฐœ๋…:** [[PHP Switch]], [[PHP Exceptions]], [[PHP Looping]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** switch์˜ PHP 8 ๋Œ€์•ˆ โ€” ๋ฐ˜๋ณต๋ฌธ(Looping) ์„น์…˜์œผ๋กœ ์ด์–ด์ง. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” PHP match Expression โ€” https://www.w3schools.com/php/php_match.asp ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP match Expression" page (Astra wiki-curation, P-Reinforce v3.1 format).