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.