search
Homephp教程php手册PHP中通过trigger_error触发PHP错误示例,trigger_errorphp

PHP中通过trigger_error触发PHP错误示例,trigger_errorphp

PHP中通过trigger_error触发PHP错误示例

【错误抑制符@】

除了 php.ini 中 error_reporting 和 display_errors 的设置、error_reporting() 函数、ini_set() 函数 外,还可以使用错误抑制符@ 屏蔽错误的输出。

@ 加在任何会产生错误的表达式之前。

【通过 trigger_error 触发 PHP 错误】

触发错误的功能不只限于 PHP 解析器,还可以通过 trigger_error() 函数触发错误,类似于异常中抛出的异常一样,抛出了一个错误,可以协助调试代码。

【例】

复制代码 代码如下:
$num1 = 1;
$num2 = '2';
if(!(is_numeric($num1) && is_numeric($num2))){
    //手动抛出通知级别的错误
    trigger_error('num1 和 num2 必须为合法数值', E_USER_NOTICE);
}else{
    echo $num1 + $num2;
}

echo '
程序继续向下执行';

输出:
复制代码 代码如下:
3
程序继续向下执行

而:
复制代码 代码如下:
$num1 = 1;
$num2 = '2a';
if(!(is_numeric($num1) && is_numeric($num2))){
    //手动抛出通知级别的错误
    trigger_error('num1 和 num2 必须为合法数值', E_USER_NOTICE);
}else{
    echo $num1 + $num2;
}

echo '
程序继续向下执行';

输出:
复制代码 代码如下:
( ! ) Notice: num1 和 num2 必须为合法数值 in D:\practise\php\Error\error1.php on line 6

程序继续向下执行

【其他】当数据库连接不上时等严重错误时,可以手动抛出错误 —— 用 E_USER_ERROR 取代 PHP 内置的E_WARNING 警告。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment