Ways to catch errors in PHP

Source: Internet
Author: User
Tags php catch error nginx ssl
This article mainly introduces the method of error in the PHP capture, has a certain reference value, now share to everyone, the need for friends can refer to

PHP Catch Error

    • Suppress error output

error_reporting (0);
    • Setting the error handler

Set_error_handler (' ErrorHandler ');
    • Functions that run at the end of the script

Register_shutdown_function (' Fatalerrorhandler ');
    • Error handling

/** * @param int    $err _no      Error code * @param string $err _msg  Error message * @param string $err _file    Error file * @param int
   $err _line     Error Line number * @return string */function errorhandler ($err _no = 0, $err _msg = ', $err _file = ', $err _line = 0) { c6/> $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 ()    {        //A warning error occurred here, departure ErrorHandler        echo $undefinedVarible;    }}
    • Here is a warning error, captured by ErrorHandler

$demo _1 = new Democlass_1 (); $demo _1->index ();
    • A fatal error occurred and the script stopped running triggering Fatalerrorhandler

$demo _2 = new Democlass_2 (); $demo _2->index ();

Output after opening test.txt:

[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:67stack trace: #0 {main}  thrown |/users /darry/htdocs/test.php | 67

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related recommendations:

Nginx SSL Fast bidirectional Authentication configuration (script)

Recursion of PHP Data structure basics

About the use of thinkphp behavior

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.