Use Register_shutdown_function function in PHP to intercept fatal error example, fatalerror_php tutorial

Source: Internet
Author: User
Tags blank page

PHP uses the Register_shutdown_function function to intercept the fatal error example, FatalError


When we do projects, we occasionally get fatal error because we are not careful. If Display_errors is set to OFF, the user will see a blank page. If set to ON, the fatal error message will appear (of course, the normal person will not do so).

So what can we do to intercept the fatal error in advance and feed it back to the user in our own custom friendly form? There is a function called Register_shutdown_function in PHP that allows us to set up another function that can be called when execution is closed. That is, the function will be called when the execution of our script completes or the Accidental death causes PHP execution to close.
Let's take a look at the following example:
Copy the Code code as follows:
<?php
$flag = false;
function Deal_error () {
Global $flag;
if (! $flag) {
Die ("coarse problem, please try again later");
}
return false;
}
Register_shutdown_function ("Deal_error");
will fail because of a fatal error
$obj = new Notexistclass (); Introduce undefined classes
Require ('./test.php ');
$flag = true;

At the entrance of the program, we set flag to false and finally set to true to indicate that the program is executing properly. If flag is not true at last, the description dies somewhere in the middle, and register_shutdown_function is called to output our custom error results.

If the class above does not define, introduce a nonexistent file (must use require or require_once), etc., it will cause fatal error. Of course, if your program is missing a punctuation or more than a special character, then there is no way.

http://www.bkjia.com/PHPjc/987895.html www.bkjia.com true http://www.bkjia.com/PHPjc/987895.html techarticle using the Register_shutdown_function function in PHP to intercept the fatal error example, FatalError we occasionally have fatal error when doing a project because of an imprudent appearance. If Display_errors is set to ...

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