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.