In addition to these, we have to add environmental errors, which are not due to the any fault of the programmer, but the result of the environmental factors beyond the programmer's control. Even if we ensure that the ode is bug-free, that doesn't guarantee that it will be necessarily run without errors. Unfortunately, programmers often have to rely on factors beyond their control (such as included files, for example).
As you can probably guess we are most interested in runtime errors. Definitely, we have to eliminate all syntax errors because our scripts won't work with them and of course we do not need to have any logical error, but this is more of a dream, than a goal. Thus, we need to deal mostly with runtime errors. That is, situations when script works (has no or syntax errors), but cannot perform its functions and interrupts with an unpleasant error.
Error level: Error message in filename on line #Here is an example:
Warning: fopen("abcdef.test", "r") - No such file or directory in /somedir/somesubdir/script.php on line 10
There are four basic error levels in PHP. There are:
integer error_reporting(level);This function takes only one argument the level of errors PHP reports. The possible arguments are in the table below:
| Value | Description |
|---|---|
| E_ALL | All errors and warnings |
| E_ERROR | fatal run-time errors |
| E_WARNING | run-time warnings (non-fatal errors) |
| E_PARSE | compile-time parse errors |
| E_NOTICE | run-time notices (these are warnings which often result from a bug in your code, but it's possible that it was intentional (e.g., using an uninitialized variable and relying on the fact it's automatically initialized to an empty string) |
| E_CORE_ERROR | fatal errors that occur during PHP's initial startup |
| E_CORE_WARNING | warnings (non-fatal errors) that occur during PHP's initial startup |
| E_COMPILE_ERROR | fatal compile-time errors |
| E_COMPILE_WARNING | compile-time warnings (non-fatal errors) |
| E_USER_ERROR | user-generated error message |
| E_USER_WARNING | user-generated warning message |
| E_USER_NOTICE | user-generated notice message |
Using this function we can get rid of annoying warnings about uninitalized variables and also hide inner structure from users by preventing displaying error messages like "Warning: file 'data/logins.txt' not found.". If argument level is set to zero, it ensures that no PHP error/warning messages will be invoked.
There is another way to suppress an error message generated by a function. All we need to do is to use @ sign in front of the function name. For example, if we do not want to see an error message about a missing file we can use code like this:
$fp = @fopen("test.txt", "r");
Please notice that this sign suppresses only this function error message.
function print_error_message($msg)
{
echo "<center>".
"<div style='color:red; background-color:lightblue; border-style:double;
text-align:left; font-size:130%; padding:1px;'>".
"Sorry $msg </div></center>";
die("</body></html>\n");
}
error_log(message, type[, destination, extra_headers]);The first argument is the error message to be logged. The output can be directed to one of four different targets:
In order to set PHP's system log file you need to edit php.ini file. Set the following two variables:
log_errors = On error_log = c:\temp\phperror.logThe first line makes the PHP log errors and the second assigns a file for the error messages. You can decide to keep PHP error log together with Apache log to do that set the second line to be
error_log = syslog
The following example shows how to hide a real error message from user, print a nice looking message instead, and save real error report to a log file:
Please consult official documentation for more details.