How to catch errors in PHP
This article mainly introduces the method of catching errors in PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it.
PHP catching errors
-
Disable error output
error_reporting(0);
-
Set error handler
set_error_handler('errorHandler');
-
function to run at the end of the script
register_shutdown_function('fatalErrorHandler');
-
Error handling
/** * @param int $err_no 错误代码 * @param string $err_msg 错误信息 * @param string $err_file 错误文件 * @param int $err_line 错误行号 * @return string */ function errorHandler($err_no = 0, $err_msg = '', $err_file = '', $err_line = 0) { $log = [ '['.date('Y-m-d h-i-s').']', '|', $err_no, '|', $err_msg, '|', $err_file, '|', $err_line ]; $log_path = './test.txt'; error_log(implode(' ',$log)."\r\n",3, $log_path); }
-
Catching fatal errors
function fatalErrorHandler() { $e = error_get_last(); var_export($e); switch ($e['type']) { case 1: errorHandler($e['type'], $e['message'], $e['file'], $e['line']); break; } }
class DemoClass_1 { public function index() { //这里发生一个警告错误,出发errorHandler echo $undefinedVarible; } }
-
A warning error occurred here and was captured by errorHandler
$demo_1 = new DemoClass_1(); $demo_1->index();
-
A fatal error occurs and the script stops running and triggers fatalErrorHandler
$demo_2 = new DemoClass_2(); $demo_2->index();
After opening test.txt, the output is:
[2018-06-12 05-49-11] | 8 | Undefined variable: undefinedVarible | /Users/darry/htdocs/test.php | 57 [2018-06-12 05-49-11] | 1 | Uncaught Error: Class 'DemoClass_2' not found in /Users/darry/htdocs/test.php:67 Stack trace: #0 {main} thrown | /Users/darry/htdocs/test.php | 67
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
related suggestion:
Nginx SSL fast two-way authentication configuration (script)
Recursion based on PHP data structure
About the use of thinkphp behavior
The above is the detailed content of How to catch errors in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...

Regarding the reason why RedisTemplate.opsForList().leftPop() does not support passing numbers. When using Redis, many developers will encounter a problem: Why redisTempl...
