Handling Error Messages in PHP

Leave a Comment
PHP tries to be helpful when problems arise by providing error messages. It provides the following types of messages:

Error message: You receive this message when the script has a problem that prevents it from running. The script displays an error message and stops running. The message contains as much information as possible to help you identify the problem. The following is a common error message:
Parse error: parse error in c:\test.php on line 6
Often, you receive this error message because you’ve forgotten a semicolon, a parenthesis, or a curly brace.

Warning message: You receive a warning message when the script sees a problem but the problem does not prevent the script from running. Warning messages do not mean the script can’t run; they indicate that PHP believes something is probably wrong. You should identify the source of the warning and then decide whether it needs to be fixed. It usually does. For example, you see the following message if you don’t include a variable name in the print_r statement — print_r() rather than print_r($varname).
Warning: print_r() expects at least 1 parameter, 0 given in d: test1.php on line 9
Because this is a warning, not an error, the script continues to execute the statements after the print_r statement. However, a warning usually indicates a more serious problem than a notice. In this case, you need to fix the problem.

Notice: You receive a notice when PHP sees a condition that may be an error or may be perfectly okay. One common condition that produces a notice is echoing variables that don’t exist. Here’s an example of what you might see in that instance:
Notice: Undefined variable: age in testing.php on line 9
Error messages, warning messages, and notices all indicate the filename causing the problem and the line number where the problem was encountered.

The types of error messages that are displayed depend on the error level that PHP is set to. You need to see all the error messages, but you may not want to see all the warnings and notices. (Often the only problem with a notice is the unsightly notice; the code is working correctly.) Or, you may want warning messages and notices displayed during development but not after customers are using the application. Or, you may want to send all the error messages to a log file, rather than have them output for users to see.

Changing the error level for your Web site

The error level for your Web site is defined in the php.ini file. You can change the error level if you are the PHP administrator and have access to the php.ini file. If you are not the administrator (which will be the case if, for example, you are using a Web hosting company), you can change the error level for each script, as described in the next section.

To see what the current error level is, open php.ini in an editor and look for a line similar to the following:
error_reporting = E_ALL; display all errors, warnings and notices
This statement causes all errors, warnings, and notices to be displayed. This setting is useful when you’re developing the script. However, when you release the script for users, you probably don’t want notices displayed.

In the preceding example, notice that there is a semicolon (;) after E_ALL but not at the beginning of the line. The semicolon is the character that indicates a comment in the php.ini file. Therefore, the text on the line after the semicolon is just a comment, not part of the statement. If there were a semicolon at the beginning of the line, the entire line would be a comment, and the statement would not be in effect.

When you look in your php.ini file, you will probably find several statements like the preceding line, except with semicolons at the beginning of the lines.

These statements are included as examples, not as statements that execute. Look for the statement without a semicolon in front of it to see which statement is currently active.

E_ALL is a built-in PHP constant that refers to all errors, warnings, and notices. E_NOTICE is a built-in constant representing notices. You can use these two constants in the following statement:
error_reporting = E_ALL & ~E_NOTICE
E_ALL tells PHP to display all errors, warnings, and notices. However, the second term ~E_NOTICE tells PHP not to display notices. The result is that only errors and warnings are displayed. This method of specifying the errors to be displayed is shorter than listing all the types of errors that you want to display.

The two statements shown in this section are used most often. You can use other constants to specify error levels, but E_ALL and E_NOTICE are usually sufficient for most scripts. You can find a listing of all the constants in the php.ini file. For a complete discussion of error levels, check out the PHP
online manual.

You can stop error reporting all together. You may not want users to see any of the PHP-generated error or warning messages because they may contain compromising information. Usually if you do this, you want to save error messages in a log instead, as described later in this chapter in the section, “Sending messages to a log.”

To turn off error reporting, find the line that says display_errors = On in php.ini and change On to Off.

You need to restart your Web server before any changes you make in php.ini will go into effect.

Changing the error level for a script

If you want to set the error level for a particular script, add a statement with the following format to the beginning of the script:
error_reporting(OPTIONS);
The OPTIONS in the statement are the built-in constants discussed in the preceding section. For example, you can have all errors, warnings, and notices displayed in the script by adding the following statement:
error_reporting(E_ALL);
Suppose the setting in php.ini is set to E_ALL. You may be satisfied with that level while developing your scripts, but then want to stop displaying notices when users start running your scripts. To override the php.ini setting, you can add the following statement to the scripts after they are finetuned and ready to go:
error_reporting(E_ALL & ~E_NOTICE);
You can set error reporting so that no messages are displayed by using the following statement:
error_reporting(0);
Sometimes you want to turn error and warning messages off when your scripts are complete and being used by the world. You may not want users to see the error messages that PHP sends because the information in the PHP messages can represent a security issue, but you may want to see any error messages from PHP yourself. You can turn error reporting off by using a setting of zero, but log the error messages to a file at the same time. Users don’t see the messages, but you can look at them. Sending messages to a log is described in the next section.

0 comments:

Post a Comment