Exception Handling in PHP

Website error results

    • E_ALL all errors
    • E_ERROR Fatal runtime error (program cannot be run error)
    • E_WARNING warning level error (the program can continue to execute)
    • E_NOTICE prompt level error
    • E_STRICT encoding standardization warning
    • E_CORE_ERROR PHP initialization process error at startup
    • E_CORE_WARNING Warning during initialization at PHP startup
    • E_COMPILE_ERROR Fatal compile-time error
    • E_COMPILE_WARNING warning error when compiling
    • E_USER_ERROR User-defined fatal error
    • E_USER_WARNING User-defined warning error
    • E_USER_NOTICE User-defined prompt


wrong option

    • Use the error suppressor @, which can only block single-line errors and is inefficient
    • Modify the display_errors option in the php.ini configuration file, set it to on to display errors, and set it to off to not display errors.
    • Modify the error_reporting option in the php configuration file to define any type of error output or no output
    • Using the error_reporting() function can achieve the same effect as the error_reporting option in the configuration file, and the set value is also the same
      • 例子:error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING)

Let the people who should see see the error

    • Step 1: Turn off the display_errors setting in the configuration file to off
    • Step 2: Set the type of storage error, set the error_reporting option in the configuration file, and specify the type of error that needs to be stored
    • Step 3: Turn on the error logging switch and set log_errors=on
    • Step 4: Set the location for error logging
      • error_log=syslog log errors to the system log


Customize how errors are handled

set_error_handker(); set a user-defined error handling function

Parameter 1: Pass in a callback function

Parameter 2: optional parameter, set the wrong type

<?php

   set_error_handler('myError');
      //Use the function to control the wrong output or not output
   echo $name;

   mysqli_connect('localhost','root','daxiong');

      function myError($error_no,$error_str,$error_file,$error_line){
             echo 'Current error number:'.$error_no.'<br/>Current error content:'.$error_str.'<br/>The error file of the current error is in:'.$error_file.'<br/>The current error line: '.$error_line;
      }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895760&siteId=291194637