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