Home Backend Development PHP Tutorial How to catch errors in PHP

How to catch errors in PHP

Jul 07, 2018 pm 04:04 PM
centos mysql nginx php redis

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);
Copy after login
  • Set error handler

set_error_handler('errorHandler');
Copy after login
  • function to run at the end of the script

register_shutdown_function('fatalErrorHandler');
Copy after login
  • 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);
}
Copy after login
  • 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;
    }
}
Copy after login
class DemoClass_1
{
    public function index()
    {
        //这里发生一个警告错误,出发errorHandler
        echo $undefinedVarible;
    }
}
Copy after login
  • A warning error occurred here and was captured by errorHandler

$demo_1 = new DemoClass_1();
$demo_1->index();
Copy after login
  • A fatal error occurs and the script stops running and triggers fatalErrorHandler

$demo_2 = new DemoClass_2();
$demo_2->index();
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

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 to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

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

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

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

Redis's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

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? Why is the return value empty when using RedisTemplate for batch query? Apr 19, 2025 pm 10:15 PM

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

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

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.

In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? Apr 19, 2025 pm 10:57 PM

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

Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Apr 19, 2025 pm 10:27 PM

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

See all articles