Home > Backend Development > PHP Tutorial > Example of triggering PHP errors through trigger_error in PHP_PHP tutorial

Example of triggering PHP errors through trigger_error in PHP_PHP tutorial

WBOY
Release: 2016-07-13 09:49:17
Original
1239 people have browsed it

Examples of triggering PHP errors through trigger_error in PHP

This article mainly introduces examples of triggering PHP errors through trigger_error in PHP. This article introduces the error suppressor @ and triggering PHP through trigger_error. Error examples, friends in need can refer to them

Example of PHP error 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.

 @ prepended to any expression that would generate 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 and can assist in debugging the code.

【Example】

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:

The code is as follows:

 3

The program continues to execute downwards

And:

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:

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/1020270.htmlTechArticleExamples of PHP errors triggered by trigger_error in PHP. This article mainly introduces examples of PHP errors triggered by trigger_error in PHP. This article Introduced the error suppressor @ and the trigger_error trigger...
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