--- id: php-error-handling title: "PHP Error Handling" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["set_error_handler", "trigger_error", "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", "error-handling"] raw_sources: ["https://www.w3schools.com/php/php_error_handling.asp"] applied_in: [] github_commit: "" --- # [[PHP Error Handling]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `set_error_handler()` REPLACES PHP's built-in error handler for the rest of the script β€” meaning ANY error (even something as ordinary as referencing an undefined variable) routes through your custom function instead of the default warning, which is why the custom handler in the source catches `echo($test);` (an undefined-variable notice) just as easily as a triggered error. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`die()`** β€” simplest error handling: stop the script and print a message (e.g., after checking `file_exists()`). [S1] - **Custom error handler function** β€” accepts (error_level, error_message, [error_file, error_line, error_context]) β€” minimum 2, up to 5 parameters. [S1] - **Error report levels** β€” `E_WARNING` (2, non-fatal), `E_NOTICE` (8), `E_USER_ERROR` (256, fatal user-generated), `E_USER_WARNING` (512), `E_USER_NOTICE` (1024), `E_RECOVERABLE_ERROR` (4096), `E_ALL` (8191). [S1] - **`set_error_handler("functionName", errorLevel)`** β€” installs a custom handler, optionally scoped to a specific error level. [S1] - **`trigger_error($message, $level)`** β€” manually raises a custom error, useful for flagging invalid user input. [S1] - **`error_log()`** β€” sends error logs to a file, the server's system, or (with the mail-mode parameter) directly to an email address. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic die(): `if(!file_exists("welcome.txt")) { die("File not found"); } else { $file=fopen("welcome.txt","r"); }`. [S1] - Custom handler function: `function customError($errno, $errstr) { echo "Error: [$errno] $errstr
"; echo "Ending Script"; die(); }`. [S1] - Registering the handler: `set_error_handler("customError");`. [S1] - Triggering a custom error: `if ($test>=1) { trigger_error("Value must be 1 or below", E_USER_WARNING); }`. [S1] - Scoped handler (only E_USER_WARNING): `set_error_handler("customError", E_USER_WARNING);`. [S1] - Emailing an error log: `error_log("Error: [$errno] $errstr", 1, "someone@example.com", "From: webmaster@example.com");`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **μ»€μŠ€ν…€ ν•Έλ“€λŸ¬κ°€ λͺ¨λ“  μ—λŸ¬λ₯Ό κ°€λ‘œμ±”**: 별도 μ—λŸ¬ λ ˆλ²¨μ„ μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ μ •μ˜λ˜μ§€ μ•Šμ€ λ³€μˆ˜ μ°Έμ‘° 같은 μ‚¬μ†Œν•œ κ²ƒκΉŒμ§€ λͺ¨λ‘ μ»€μŠ€ν…€ ν•Έλ“€λŸ¬λ‘œ λΌμš°νŒ…λœλ‹€λŠ” 점이 μ‹€ν—˜ 예제둜 검증됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” νŠΉμ • μ—λŸ¬ λ°œμƒ μ‹œ μ›Ήλ§ˆμŠ€ν„°μ—κ²Œ 이메일 μ•Œλ¦Όμ„ λ³΄λ‚΄λŠ” 것이 μ‹€μ „ λͺ¨λ‹ˆν„°λ§μ˜ λŒ€ν‘œ 사둀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Custom error handler scoped to a specific error level (PHP): ```php function customError($errno, $errstr) { echo "Error: [$errno] $errstr
"; echo "Ending Script"; die(); } set_error_handler("customError", E_USER_WARNING); $test = 2; if ($test >= 1) { trigger_error("Value must be 1 or below", E_USER_WARNING); } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP Exceptions]], [[PHP Exception]], [[PHP File Open]] - **μ°Έμ‘° λ§₯락:** 절차적 μ—λŸ¬ 처리(die/custom handler/trigger_error) β€” 객체지ν–₯ μ˜ˆμ™Έ 처리(Exception Handling) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP Error Handling β€” https://www.w3schools.com/php/php_error_handling.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP Error Handling" page (Astra wiki-curation, P-Reinforce v3.1 format).