So, first let's define what a errors in the PHP.
PHP supports the following levels of errors:
In fact - it's just a constant that is used to determine the level of error handling, build bit-masks.
Constants are "speaking" names.
Looking at a constant - we can say that the error level E_PARSE arises in the case of syntax error, E_NOTICE - a reminder to the programmer to a violation of "good style" of programming in PHP.
A few examples:
When the database connection MySQL (or another) fails - PHP interpreter reports an error of level E_WARNING
Warning: mysql_connect(): Access denied for user: 'VVingless@localhost' (Using password: YES)
In /home/mysite/index.php (line 83)
Note: In order for the PHP reported errors - PHP must be configured properly:
display_errors flag should be included - 1, error_reporting directive should specify that you want to display an error level of E_WARNING (of course desirable, and others).
If the values ??of these directives do not meet your needs - you can try to install them yourself, put in a folder with a script file.
Htaccess (the point at the beginning of the name required), similar to the following:
php_flag display_errors on
php_value error_reporting "E_ALL & ~ E_NOTICE"
This means that error messages will be displayed, and all levels except E_NOTICE
When the programmer allows a syntax error - PHP interpreter reports an error level E_PARSE
Parse error: parse error, unexpected '(', expecting T_STRING in / home / mysite / index.php on line 150
But the most interesting for us to levels of error - E_USER_ERROR and E_USER_WARNING. As is clear from the title - it is the levels of errors that can be installed by the user. To do this, there is a function trigger_error () - with her help, you can inform the user about the incident as it does PHP.
As we know from the manual on PHP - the function trigger_error () takes two parameters.
void trigger_error ( string error_msg [, int error_type])
The first option - a text error message such as "file not found".
The second parameter - determines the level of error.
The function trigger_error () only works with the family of error E_USER - this means that you can set the error level E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE and can not fix the error of level E_WARNING.
The second parameter is optional and defaults to E_USER_NOTICE.
For example, our data feeds are stored in a file news.txt, and if the file is not found - should report an error. Source code would look something like this:
As a result, the PHP interpreter reports an error level E_USER_NOTICE
Notice: News file not found in /home/mysite/index.php on line 47
But what we do for you? To begin with the fact that if in the php.ini file or. Htaccess directives have been established
php_value log_errors "1"
php_value log_errors_max_len "1024"
php_value error_log "/home/mysite/my.log"
So the file / home / mysite / my.log will be automatically added to the record of the incident.
[23-Mar-2004 13:52:03] PHP Notice: News file not found in /home/mysite/index.php on line 47
Next, using the set_error_handler (), we can install your own error handler encountered during the execution of PHP script.
As we know from the manual - in PHP 4, the function accepts a single string parameter - the name of the function to be executed whenever an error occurs. PHP 5 allows you to set one more parameter - the type of errors that will be handled by our handler. The function returns a string - the name of the handler function, which was set up to this point.
string set_error_handler ( callback error_handler [, int error_types])
User-defined function that will handle the error, can take the following input parameters:
- Code-level bugs
- A string interpretation of the error
- The name of the file where the error occurred
- A line where the error occurred
It should also be noted that this function can not handle the error level E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
This is due to the fact that these levels of error occur before the interpreter receives information about the custom error handler.
So, we declare our function
Note:each is more or less surround the script is usually divided into multiple files for ease of working with him.
How to organize the modularity of the program - another topic.
Now, I want only to provide general advice to a separate configuration file, which will connect at the beginning of the program by using include, either through directives auto_prepend_file.
This file can be put, and our handler. Set the error handler should be realized as close as possible to the beginning of the program, preferably at the beginning.
To make sure that it really works - create a new PHP file, and try to run it
The contents of the file myerrortest.php
The result of processing the file would be:
An error occurred News file not found (1024)
/ home / mysite / myerrortest.php (12)
Now we have a function that retrieves all occurring errors. Think of how we can use it.
We will handle the error levels
The first three errors in the finished good program should not happen at all, so for them we will only notify the user error text output to the screen.
So you can work until the script is under development, and then reporting them, you can either disable, or write to the log-file.
As for the other two - you guessed it - they can come in handy there. We are going to cause errors of these levels if necessary.
Let's say - the error level E_USER_ERROR - will call when the error message should go to the log-file and be sent by e-mail administrator (for example - failed to execute SQL query or parva lack of access to the appropriate file).
Error level E_USER_NOTICE will be called when a "soft" errors (for example - the user has filled out a form incorrectly, or requested from the database that does not exist).
Now, our error-handling function would look something like this:
Now, describe the utility functions
Finally, an example of using
In order to earn an example - just copy the PHP-file three previous code block. Do not forget to assign permissions to the log-file 777 so that the script could work with him, to prescribe the correct path and enter your e-mail.
You can enable debug mode by setting the variable DEBUG to 1.
This is a fairly simple example, the theme can be developed.