PHP 错误处理机制,php处理机制
PHP 错误处理机制,php处理机制
在日常的项目开发过程中,总是会出现一些我们意想不到的异常错误,如果我们对此没有进行相对完善的处理,那么程序看上去也很不专业,也很可能就会成为别人攻击系统的有效信息;有些错误异常会终止脚本执行,这个时候如果没有一些错误提示信息的话,那么我们只能从头开始看代码了,要知道项目中成百上千行的代码对我们来说是件多么恐怖的事情啊,那么我们如何在项目开发的过程中快速准确地定位到异常、错误呢,并进行相应的处理,本文由自己对错误、异常处理的了解,再此分享与大家相互学习交流,并作为一个备忘。
系统错误处理器:
PHP正常情况下,错误会正常的输出,但在一些框架中,可能会影响错误的输出,可能是框架本身有自己的处理机制,也可能代码中作处理了,一般是这几个函数设置:
1.error_reporting(); 设置PHP 的报错级别并返回当前级别
error_reporting(report_level)
如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值:
值 |
常量 |
描述 |
1 |
E_ERROR |
致命的运行时错误。不能回收此错误。脚本被中断执行。 |
2 |
E_WARNING |
非致命的运行时警告。脚本不被中断执行。 |
4 |
E_PARSE |
编译时分析错误。解析应该只由分析器生成的错误 |
8 |
E_NOTICE |
运行时通知。该脚本发现可能是一个错误,但通常运行一个脚本时,也可能发生 |
16 |
E_CORE_ERROR |
致命错误在PHP启动时。这就好比在PHP核心的E_ERROR |
32 |
E_CORE_WARNING |
在PHP启动时警告。这就好比在PHP核心的E_WARNING |
64 |
E_COMPILE_ERROR |
致命的编译时错误。这就好比通过了Zend脚本引擎产生的E_ERROR |
128 |
E_COMPILE_WARNING |
非致命编译时警告。这就好比通过了Zend脚本引擎产生E_WARNING |
256 |
E_USER_ERROR |
致命的用户生成的错误,这类似于程序员使用PHP函数 trigger_error() 设置的 E_ERROR |
512 |
E_USER_WARNING |
非致命的用户生成的警告,这类似于程序员使用PHP函数 trigger_error 设置的 E_WARNING |
1024 |
E_USER_NOTICE |
用户生成的通知,这类似于程序员使用PHP函数trigger_error 设置的 E_NOTICE |
2048 |
E_STRICT |
运行时通知。PHP建议改变你的代码,以帮助该代码的互操作性和兼容性 |
4096 |
E_RECOVERABLE_ERROR |
可捕获的致命错误,类似 E_ERROR,但可被用户定义的处理程序捕获(参见 set_error_handler()) |
8191 |
E_ALL |
所有的错误和警告,除级别E_STRICT(在PHP6.0中,E_STRICT将是E_ALL的一部分) |
这里值得注意的是,$level为0的时候是关闭错误输出,也就是任何错误都不会输出。
2.set_error_handler()
定义和用法
set_error_handler() 函数设置用户自定义的错误处理函数。
该函数用于创建运行时期间的用户自己的错误处理方法。
该函数会返回旧的错误处理程序,若失败,则返回 null。
语法
set_error_handler(error_function,error_types)
参数 |
描述 |
error_function |
必需。规定发生错误时运行的函数。 |
error_types |
可选。规定在哪个错误报告级别会显示用户定义的错误。默认是 "E_ALL"。 |
提示:如果使用了该函数,会完全绕过标准的 PHP 错误处理函数,如果必要,用户定义的错误处理程序必须终止 (die() ) 脚本,
注释:如果在脚本执行前发生错误,由于在那时自定义程序还没有注册,因此就不会用到这个自定义错误处理程序。
测试代码如下:
/** * * @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);
//通过上案例可以得知,在注册 my_error 方法时,系统会自动覆盖原有的错误处理 error_fuction() 方法
以上程序运行结果:
自定义错误触发器
定义和用法
trigger_error() 函数创建用户定义的错误消息。
trigger_error() 用于在用户指定的条件下触发一个错误消息。它与内建的错误处理器一同使用,也可以与由 set_error_handler() 函数创建的用户自定义函数使用。
如果指定了一个不合法的错误类型,该函数返回 false,否则返回 true。
语法
trigger_error(error_message,error_types)
参数 |
描述 |
error_message |
必需。规定错误消息。长度限制为 1024 个字符。 |
error_types |
可选。规定错误消息的错误类型。 可能的值:
|
/** * * @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); }
运行结果如下:
WARNING:
错误编号:512
错误信息:这是错误啊
以上所述就是本文的全部内容了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
