Home  >  Article  >  Backend Development  >  Example of triggering PHP errors through trigger_error in PHP, trigger_errorphp_PHP tutorial

Example of triggering PHP errors through trigger_error in PHP, trigger_errorphp_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:48:441594browse

Examples of PHP errors triggered by trigger_error in PHP, trigger_errorphp

Examples of PHP errors triggered by trigger_error in PHP

【Error suppressor @】

In addition to the error_reporting and display_errors settings, error_reporting() function, and ini_set() function in php.ini, you can also use the error suppressor @ to mask error output.

@ before any expression that would produce an error.

【Trigger PHP error through trigger_error】

The function of triggering errors is not limited to the PHP parser. You can also trigger errors through the trigger_error() function. Similar to the exception thrown in the exception, an error is thrown, which can assist in debugging the code.

【Example】

Copy code The code is as follows:
$num1 = 1;
$num2 = '2';
if(!(is_numeric($num1) && is_numeric($num2))){
//Manually throw notification level errors
​ trigger_error('num1 and num2 must be legal values', E_USER_NOTICE);
}else{
echo $num1 $num2;
}

echo '
The program continues to execute downwards';

Output:
Copy code The code is as follows:
3
The program continues to execute downwards

And:
Copy code The code is as follows:
$num1 = 1;
$num2 = '2a';
if(!(is_numeric($num1) && is_numeric($num2))){
//Manually throw notification level errors
​ trigger_error('num1 and num2 must be legal values', E_USER_NOTICE);
}else{
echo $num1 $num2;
}

echo '
The program continues to execute downwards';

Output:
Copy code The code is as follows:
( ! ) Notice: num1 and num2 must be legal values ​​in D:practisephpErrorerror1.php on line 6

The program continues to execute downwards

【Others】When there is a serious error such as database connection failure, you can manually throw an error - use E_USER_ERROR to replace PHP's built-in E_WARNING warning.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1021092.htmlTechArticleExamples of PHP errors triggered by trigger_error in PHP, trigger_errorphp Examples of PHP errors triggered by trigger_error in PHP [error suppressor @] In addition to error_reporting and displ in php.ini...
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