The code is as follows: |
Copy code |
<? Php $ A = fopen('test.txt ', 'r '); // The file is opened without judgment. If the file does not exist, an error is returned. ?> |
The correct statement should be as follows:
The code is as follows: |
Copy code |
<? Php If(file_exists('test.txt ')){ Using fw.fopen('test.txt ', 'r '); // Close after use Fclose ($ f ); } ?> |
I. PHP error handling methods A. Simple die () statements;
Equivalent to exit ();
Example:
The code is as follows: |
Copy code |
If (! File_exists('aa.txt ')){ Die ('file does not exist '); } Else { // Perform the operation } // If the die () is triggered, the echo is not executed here. Echo 'OK '; |
Concise syntax:
The code is as follows: |
Copy code |
File_exits('aaa.txt ') or die ('file does not exist '); Echo 'OK '; |
B. Custom errors and error triggers
1. Error Processor (custom error, usually used for syntax error handling)
Create a custom error function (processor). This function must be able to process at least two parameters (error_level and errormessage), but can accept up to five parameters (error_file, error_line, error_context)
Syntax:
Function error_function ($ error_level, $ error_message, $ error_file, $ error_line, $ error_context)
// After creation, you need to rewrite set_error_handler (); function
Set_error_handler ('error _ function', E_WARNING); // Here, error_function corresponds to the custom processor name created above, and the second parameter is the error level of the Custom error Processor;
Error Report level (learn more)
These error reporting levels are different types of errors that the error handler aims to handle:
Value constant description
2 E_WARNING: non-fatal run-time error. Do not pause script execution.
8 E_NOTICE Run-time notification.
An error may occur when the script runs normally.
256 E_USER_ERROR fatal user-generated error. This is similar to the E_ERROR set by the programmer using the PHP function trigger_error.
512 E_USER_WARNING non-fatal user-generated warning. This is similar to the E_WARNING set by the programmer using the PHP function trigger_error.
1024 E_USER_NOTICE user-generated notifications. This is similar to the E_NOTICE set by the programmer using the PHP function trigger_error.
4096 E_RECOVERABLE_ERROR: a fatal error that can be captured. Similar to E_ERROR, but can be captured by a user-defined handler. (See set_error_handler ())
8191 all errors and warnings except the level E_STRICT.
(In PHP 6.0, E_STRICT is part of E_ALL)
2. Error triggers (generally used to handle logical errors)
Requirement: for example, to receive an age, if the number is greater than 120, it is considered an error.
Traditional method:
'If ($ age> 120 ){
Echo 'age error'; exit ();
}
Trigger:
'If ($ age> 120 ){
// Trigger_error ('error information' [, 'error level']); the error level is optional and is used to define the error level.
// User-defined levels include E_USER_WARNING, E_USER_ERROR, and E_USER_NOTICE.
Trigger_error ('Age error'); // The default error handling method of the called System. You can also use a custom processor.
}
// Custom processor, same as above
Function myerror ($ error_level, $ error_message ){
Echo 'error text ';
}
// At the same time, you need to change the default processing function of the system.
Set_error_handler ('myerror', E_USER_WARNING); // same as above, the first parameter is the name of the custom function, and the second parameter is the error level. The error level here is generally the following: e_USER_WARNING, E_USER_ERROR, and E_USER_NOTICE]
// Now trigger_error can be used to customize the error handling function.
Exercise questions:
The code is as follows: |
Copy code |
<? Php Date_default_timezone_set ('prc '); Function myerror ($ error_level, $ error_message ){ $ Info = "error code: $ error_leveln "; $ Info. = "error message: $ error_messagen "; $ Info. = 'Occurrence Time: '. date ('Y-m-d H: I: s '); Using filename='aa.txt '; If (! $ Fp = fopen ($ filename, 'A ')){ 'File creation '. $ filename. 'Failed '; } If (is_writeable ($ filename )){ If (! Fwrite ($ fp, $ info )){ Echo 'failed to write the file '; } Else { Echo 'error message recorded successfully '; } Fclose ($ fp ); } Else { Echo 'file'. $ filename. 'Unwriteable '; } Exit (); } Set_error_handler ('myerror', E_WARNING ); Using fpw.fopen('aaa.txt ', 'r '); ?> |