PHP error handling method example

小云云
Release: 2023-03-20 20:50:01
Original
1501 people have browsed it

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 It is processed in the code, usually these function settings:

1.error_reporting(); Set the error 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 items are possible values ​​for level:

##fatal Runtime error. This error cannot be recycled. Script execution was interrupted. 2E_WARNINGNon-fatal runtime warning . Script execution is not interrupted. 4##E_COMPILE_ERROR generated by the Zend script engine## through the Zend script engine. ##512E_USER_WARNINGNon-fatal user-generated warning, similar to the E_WARNING set by the programmer using the PHP function trigger_error1024E_USER_NOTICEA user-generated notification, similar to the E_NOTICE set by the programmer using the PHP function trigger_error 2048

Value

Constant

Description

##1

E_ERROR

##E_PARSE

Compile time analysis error. Parsing should only generate errors

8

E_NOTICE

Runtime notification. The script found may be an error, but typically when running a script,

16

E_CORE_ERROR may also occur

Fatal error when starting PHP. This is like E_ERROR

32

E_CORE_WARNING

# in PHP core ## Warn on PHP startup. This is like E_WARNING

#64

# in the PHP core ##Fatal compile-time error. This is like the E_ERROR

128

E_COMPILE_WARNING

Non-fatal compile-time warning. This is like generating E_WARNING

256

E_USER_ERROR

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

E_STRICT

Runtime notification. PHP recommends changes to your code to aid interoperability and compatibility of this code

4096

E_RECOVERABLE_ERROR

Catchable fatal error, similar to E_ERROR, but can be caught by a 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 a part of)

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 will return the old error handler, or null if it fails.

Syntax

set_error_handler(error_function,error_types)

Parameters

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 function will be completely bypassed, and the user-defined error handler must terminate if necessary (die () ) Script,

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

//通过上案例可以得知,在注册 my_error 方法时,系统会自动覆盖原有的错误处理 error_fuction() 方法
以上程序运行结果:

自定义错误触发器

定义和用法

trigger_error() 函数创建用户定义的错误消息。

trigger_error() 用于在用户指定的条件下触发一个错误消息。它与内建的错误处理器一同使用,也可以与由 set_error_handler() 函数创建的用户自定义函数使用。

如果指定了一个不合法的错误类型,该函数返回 false,否则返回 true。

语法

trigger_error(error_message,error_types)


参数

描述

error_message

必需。规定错误消息。长度限制为 1024 个字符。

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 data-filtered="filtered">";
    break;
  case E_USER_WARNING:
    echo "WARNING:<br data-filtered="filtered">";
    break;
  case E_USER_NOTICE:
    echo "NOTICE:<br data-filtered="filtered">";
    break;
  default:
    break;
  }
  echo "错误编号:" . $level . " <br data-filtered="filtered">";
  echo "错误信息:" . $msg;
}
//注册错误处理器
set_error_handler(&#39;my_error&#39;);
if (89 > 8) {
  //调用错误触发器
  trigger_error(&#39;这是错误啊&#39;, E_USER_WARNING);
}
Copy after login

运行结果如下:

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

相关推荐:

php错误处理和日志记录

PHP错误与异常调试视频教程资源分享

PHP错误机制

The above is the detailed content of PHP error handling method example. For more information, please follow other related articles on the PHP Chinese website!

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