## PHP XML
( SimpleXML]( SimpleXMLR](
## PHP & AJAX
[PHP & AJAX Introduction]( PHP]( Database]( XML]( Live Search]( RSS Reader]( Poll](
## PHP Reference Manual
( Calendar Functions]( Date/Time Functions]( Directory Functions]( Error Functions]( Filesystem Functions]( Filter Functions]( FTP Functions]( HTTP Functions]( Libxml Functions]( Mail Functions]( Math Functions]( Misc Functions]( MySQLi Functions]( SimpleXML Functions]( String Functions]( XML Parser Functions]( Zip File Functions](
## PHP Theme
(
## More Resources
( Offline Manual](
PHP set_error_handler() function
---
## Definition and Usage
The set_error_handler() function sets a user-defined error handler function.
The set_error_handler() function can be used to create your own error handling logic.
**Note:** Even if set_error_handler() is set, the standard PHP error handler will still be called in cases where the error types are E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR, E_PARSE, or certain startup errors.
**Note:** If the function returns FALSE, the normal error handler will continue to run.
Syntax
```
set_error_handler(errorhandler, error_types)
```
| Parameter | Description |
|---|---|
| errorhandler | Required. Specifies the function to be called when an error occurs |
| error_types | Optional. Specifies which error types the handler function handles. Default: NULL |
The error handler function needs to accept at least two parameters: the error level and the error message. Optionally, it can accept three more parameters: the filename, line number, and context of the error.
## Technical Details
| Return Value: | Returns the previously defined error handler, or NULL on error. Returns TRUE if there is no previously defined error handler. |
|---|---|
| PHP Version: | 4.0.1+ |
## More Examples
**Example**
Use set_error_handler() to create a custom error handler:
```php
<?php
// Custom error handler function
function customError($errno, $errstr, $errfile, $errline)
{
echo "Custom error: [$errno] $errstr
";
echo "Error on line $errline in $errfile
";
}
// Set error handler
set_error_handler("customError");
// Trigger an error
echo($test);
?>
```
Output:
```
Custom error: Undefined variable: test
Error on line 5 in C:webfoldertest.php
```
**Example**
Use the third and fourth parameters to get the filename and line number of the error:
```php
<?php
// Custom error handler function
function customError($errno, $errstr, $errfile, $errline)
{
echo "Custom error: [$errno] $errstr
";
echo "Error on line $errline in $errfile
";
}
// Set error handler
set_error_handler("customError");
// Trigger an error
echo($test);
?>
```
Output:
```
Custom error: Undefined variable: test
Error on line 5 in C:webfoldertest.php
```
**Example**
Use set_error_handler() and trigger an error to handle different error levels:
```php
<?php
// Custom error handler function
function customError($errno, $errstr, $errfile, $errline)
{
echo "Custom error: [$errno] $errstr
";
echo "Error on line $errline in $errfile
";
echo "Ending Script";
die();
}
// Set error handler
set_error_handler("customError",E_USER_WARNING);
$test=2;
if ($test>1)
{
trigger_error("Value must be 1 or below",E_USER_WARNING);
}
?>
```
Output:
```
Custom error: Value must be 1 or below
Error on line 6 in C:webfoldertest.php
Ending Script
```
---
## Related Functions
- [error_reporting()](
- [trigger_error()](
- [set_exception_handler()](
- [restore_exception_handler()](
[๐ก: Hint: Use **set_error_handler()** to create your own custom error handling function!]
**Score:**
[How to become a good programmer?](
[โฎ Previous]( โฏ](
[โHome]( | [โฎ PHP Error Functions](
**Execution Time:** 0.0247s
(c) Copyright 2013-2024
[](http://www.dreamtemplate.com)[](http://www.soonmark.com)[](http://www.surlms.com)
[โ]( Chat](
๐ Categories
- โก JavaScript (1589)
- ๐ PHP (872)
- ๐ Python3 (810)
- ๐ HTML (691)
- โ๏ธ C# (650)
- ๐ Python (594)
- โ Java (552)
- โ๏ธ PyTorch (534)
- ๐ง Linux (472)
- โ๏ธ C (432)
- ๐ฆ jQuery (406)
- ๐จ CSS (377)
- ๐ XML (259)
- ๐ฆ jQuery UI (231)
- ๐ฏ Bootstrap (220)
- โ๏ธ C++ (215)
- ๐ ฐ๏ธ Angular (205)
- ๐ HTML DOM (201)
- ๐ด Redis (188)
- ๐ Web Building (142)
- ๐ Vue.js (141)
- ๐ R (131)
- ๐ผ Pandas (124)
- ๐๏ธ SQL (105)
- โ๏ธ Docker (86)
- โ๏ธ TypeScript (73)
- โ๏ธ Highcharts (70)
- ๐ AI Agent (70)
- โ๏ธ React (68)
- ๐ Node.js (65)
- โ๏ธ Machine Learning (60)
- ๐ Git (59)
- ๐ต Go (58)
- ๐ Markdown (58)
- ๐ข NumPy (55)
- ๐งช Flask (54)
- โ๏ธ Scala (53)
- ๐๏ธ SQLite (52)
- ๐ JSTL (52)
- โ๏ธ VS Code (51)
- ๐ MongoDB (49)
- ๐ Perl (48)
- ๐ Ruby (47)
- ๐ Matplotlib (47)
- โ๏ธ Uncategorized (46)
- ๐ Swift (46)
- ๐๏ธ PostgreSQL (46)
- โ๏ธ Data Structures (46)
- ๐ Playwright (46)
- ๐ iOS (45)
- ๐๏ธ MySQL (44)
- โ๏ธ LangChain (43)
- ๐ FastAPI (40)
- โ๏ธ Ionic (38)
- ๐ Design Patterns (37)
- โ๏ธ Eclipse (37)
- ๐จ CSS3 (34)
- ๐ Lua (34)
- โ๏ธ Codex (34)
- ๐ธ Django (32)
- โ๏ธ OpenCV (32)
- ๐ Rust (31)
- ๐ JSP (31)
- โ๏ธ Claude Code (31)
- ๐ Pillow (30)
- โ๏ธ OpenCode (28)
- ๐ AI Skills (27)
- ๐ Flutter (26)
- ๐ Maven (26)
- ๐จ Tailwind CSS (25)
- ๐ง TensorFlow (25)
- ๐ Servlet (24)
- ๐ Dart (23)
- ๐ Assembly (23)
- โ๏ธ Memcached (22)
- โ๏ธ SVG (22)
- โ๏ธ Electron (22)
- ๐ NLP (22)
- ๐ Regex (21)
- ๐ Android (20)
- ๐ฃ Kotlin (19)
- ๐ Julia (19)
- ๐ SOAP (17)
- ๐ Selenium (17)
- ๐ PowerShell (17)
- ๐ Sass (16)
- ๐ HTTP (16)
- ๐ Zig (15)
- ๐ AI (15)
- ๐ AJAX (14)
- ๐ Swagger (14)
- โ๏ธ Scikit-learn (13)
- โ๏ธ ECharts (13)
- โ๏ธ Chart.js (13)
- โ๏ธ Cursor (13)
- โ๏ธ SciPy (12)
- ๐ RDF (12)
- ๐ Ollama (12)
- ๐ Next.js (12)
- ๐ Plotly Dash (12)
- ๐ JSON (11)
- ๐ RESTful API (11)
- ๐ WSDL (9)
- โ๏ธ CMake (8)
- ๐ Firebug (7)
- ๐ Nginx (6)
- โธ๏ธ Kubernetes (6)
- ๐ Jupyter (6)
- ๐ LaTeX (4)
- ๐ UniApp (4)
- ๐๏ธ SQL Server (1)
YouTip