PHP error handling mechanism, php processing mechanism_PHP tutorial

WBOY
Release: 2016-07-13 09:47:13
Original
891 people have browsed it

PHP error handling mechanism, php processing mechanism

In the daily project development process, there will always be some unexpected exception errors that we don’t expect. If we don’t deal with them, If the processing is perfect, the program will look very unprofessional and may become effective information for others to attack the system; some error exceptions will terminate the execution of the script. If there is no error message at this time, then we can only start from the beginning. Looking at the code, we need to know how terrifying it is for us to have hundreds or thousands of lines of code in the project. So how can we quickly and accurately locate exceptions and errors during the project development process, and make corresponding adjustments? Processing, this article is based on my own understanding of error and exception handling, and I will share it with everyone to learn from each other and serve as a memo.

System error handler:

Under normal circumstances in PHP, errors will be output normally, but in some frameworks, it may affect the error output. It may be that the framework itself has its own processing mechanism, or it may be processed in the code, usually these are Function settings:

1.error_reporting(); Set the error reporting level of PHP and return the current level

error_reporting(report_level)

If the parameter level is not specified, the current error level will be returned. The following are possible values ​​for level:

Value

Constant

Description

1

E_ERROR

Fatal runtime error. This error cannot be recycled. Script execution was interrupted.

2

E_WARNING

Non-fatal runtime warning. Script execution is not interrupted.

4

E_PARSE

Compile time analysis error. Parsing errors should only be generated by the parser

8

E_NOTICE

Runtime notification. The script found may be a bug, but it can also happen normally when running a script

16

E_CORE_ERROR

Fatal error when starting PHP. This is like the E_ERROR in the core of PHP

32

E_CORE_WARNING

Warn when PHP starts . This is like the E_WARNING in the core of PHP

64

E_COMPILE_ERROR

Fatal compile-time error. This is like passing the Zend script engine to generate the E_ERROR

128

E_COMPILE_WARNING

Non-fatal compile-time warning. This is like passing the Zend script engine to generate E_WARNING

256

E_USER_ERROR

Fatal user-generated error, similar to the one set by the programmer using the PHP function trigger_error() E_ERROR

512

E_USER_WARNING

Non-fatal user-generated warning, similar to one set by the programmer using the PHP function trigger_error E_WARNING

1024

E_USER_NOTICE

User-generated notifications, similar to those set by programmers using the PHP function trigger_error E_NOTICE

2048

E_STRICT

Runtime notification. PHPSuggest changes to your code to help with interoperability and compatibility of that code

4096

E_RECOVERABLE_ERROR

Catchable fatal error, similar to E_ERROR, but can be caught by user-defined handler (see set_error_handler())

8191

E_ALL

All errors and warnings except level E_STRICT (In PHP6.0, E_STRICT will be E_ALL part)

It is worth noting here that when $level is 0, error output is turned off, that is, no errors will be output.

2.set_error_handler()

Definition and usage

The set_error_handler() function sets a user-defined error handling function.

This function is used to create the user's own error handling method during runtime.

This function returns the old error handler, or null on failure.

Grammar

set_error_handler(error_function,error_types)

Parameters

参数

描述

error_function

必需。规定发生错误时运行的函数。

error_types

可选。规定在哪个错误报告级别会显示用户定义的错误。默认是 "E_ALL"。

Description

error_function

Required. Specifies the function to run when an error occurs.

error_types

Optional. Specifies at which error reporting level user-defined errors are displayed. The default is "E_ALL".

Tip: If this function is used, the standard PHP error handling functions are completely bypassed, and the user-defined error handler must terminate (die()) the script if necessary,

Note: If an error occurs before the script is executed, the custom error handler will not be used because the custom program has not been registered at that time.

The test code is as follows:

/**
 *
 * @param type $error_level 错误级别
 * @param type $error_message    错误信息
 * @param type $error_file 可选 错误文件
 * @param type $error_line 可选 错误行
 * @param type $error_context 可选。规定一个数组,包含了当错误发生时在用的每个变量以及它们的值。
 */
function my_error($error_level, $error_message, $error_file, $error_line, $error_context) {
  echo date('Y-m-d H:i:s') . $error_level . $error_message . $error_file . $error_line;
  var_dump($error_context);
}
set_error_handler('my_error', E_ALL);
print_r($a);
 
Copy after login

//From the above case, we can know that when registering the my_error method, the system will automatically overwrite the original error handling error_fuction() method
The result of running the above program:

Custom error trigger

Definition and usage

The trigger_error() function creates a user-defined error message.

trigger_error() is used to trigger an error message under user-specified conditions. It is used with the built-in error handler or with user-defined functions created with the set_error_handler() function.

If an illegal error type is specified, this function returns false, otherwise it returns true.

Grammar

trigger_error(error_message,error_types)

Parameters

Description

error_message

Required. Specifies the error message. Length limit is 1024 characters.

error_types

可选。规定错误消息的错误类型。 可能的值:

  • E_USER_ERROR
  • E_USER_WARNING
  • E_USER_NOTICE
测试代码如下:

/**
 *
 * @param type $level
 * @param type $msg
 */
function my_error($level, $msg) {
  switch ($level) {
  case E_USER_ERROR:
    echo "ERROR:<br/>";
    break;
  case E_USER_WARNING:
    echo "WARNING:<br/>";
    break;
  case E_USER_NOTICE:
    echo "NOTICE:<br/>";
    break;
  default:
    break;
  }
  echo "错误编号:" . $level . " <br/>";
  echo "错误信息:" . $msg;
}
//注册错误处理器
set_error_handler('my_error');
if (89 > 8) {
  //调用错误触发器
  trigger_error('这是错误啊', E_USER_WARNING);
}
Copy after login

运行结果如下:

WARNING:
错误编号:512
错误信息:这是错误啊

以上所述就是本文的全部内容了,希望大家能够喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1027497.htmlTechArticlePHP 错误处理机制,php处理机制 在日常的项目开发过程中,总是会出现一些我们意想不到的异常错误,如果我们对此没有进行相对完善的处...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!