PHP Error Handling
When we develop programs, sometimes there are problems with the program, we can use the following methods to find the error.
Development phase: Output all error reports during development, which will help us to debug the program
Run phase: We do not let the program output any kind of error report (not to allow users to see (including technical, not skilled people))
Write error reports to the log
First, specify error report error_reporting = E_ll
Second, turn off error output Display_errors = Off
Third, turn on error logging function log_errors = On
1. Default if no error log location is specified, the default write Web server log
2. Specify a filename (writable) for the Error_log option
3. Write to the operating system log Error_log=syslog
The following code example
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><?php
// error_reporting (e_all);
Ini_set ("Display_errors", "off"); ini_set ("Error_log", "syslog"); ini_set ("Max_fileupload", 200000000); Echo ini_get ("upload_max_filesize"); Error_log ("This is an error message!!!!");
GetType ($var); Note
GetType (); Warning
gettye (); The error terminates the program running
echo "###########################<br>";
? ></span>
Of course, PHP also provides function error_get_last () to get the error message function definition and usage
The Error_get_last () function Gets the last occurrence of the error.
The function returns the last occurrence of the error as an array.
The returned array contains 4 keys and values:
[Type]-error type
[Message]-error messages
[File]-Files where the error occurred
[Line]-where the error occurred
Small example:
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><?php Echo $test; Print_r (Error_get_last ());?> output:
Array ([type] => 8 [message] => Undefined variable:test [file] => D:\WW w\test.php [line] => 2) </span>
php5.4 also provides PHP predefined variables later $php_errormsg
$php _errormsg-a previous error message
$php _errormsg variable contains the latest error message generated by PHP. This variable is only available within the scope of the error occurrence and requires that the Track_errors configuration entry be turned on (by default).
Example:
<?php
@strpos ();
echo $php _errormsg;
? >
Will output:
Wrong parameter count for Strpos ()
So it's convenient for us ... Is it helpful to debug your program and troubleshoot bugs?
These error reporting levels are different types of errors that the error handlers are designed to handle: