---
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 "<b>Error:</b> [$errno] $errstr<br>"; 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 "<b>Error:</b> [$errno] $errstr<br>";
    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).
