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 警告。

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

Atom editor mac version download
The most popular open source editor

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