Debugging Tips

Configuring Error Reporting

An error message that the PHP interpreter generates falls into one of five different categories:

  1. Parse error: A problem with the syntax of your program, such as leaving a semicolon off of the end of a statement. The interpreter stops running your program when it encounters a parse error.
  2. Fatal error: A severe problem with the content of your program, such as calling a function that hasn't been defined. The interpreter stops running your program when it encounters a fatal error.
  3. Warning: An advisory from the interpreter that something is fishy in your program, but the interpreter can keep going. Using the wrong number of arguments when you call a function causes a warning.
  4. Notice: A tip from the PHP interpreter, playing the role of Miss Manners. For example, printing a variable without first initializing it to some value generates a notice.
  5. Strict notice: An admonishment from the PHP interpreter about your coding style. Most of these have to do with esoteric features that changed between PHP 4 and PHP 5, so you're not likely to run into them too much.

By default, PHP reports all errors other than notices. The error reporting level is assigned using a set of predefined constants:

Value Name Meaning
1 E_ERROR Report fatal errors at runtime
2 E_WARNING Report nonfatal errors at runtime
4 E_PARSE Report parse errors
8 E_NOTICE Report notices, notifications that something you have done might be an error.
16 E_CORE_ERROR Report failures in the startup of the PHP engine.
32 E_CORE_WARNING Report nonfatal failures in the startup of the PHP engine.
64 E_COMPILE_ERROR Report errors in compilation
128 E_COMPILE_WARNING Report nonfatal errors in compilation
256 E_USER_ERROR Report user-triggered errors
512 E_USER_WARNING Report user-triggered warnings
1024 E_USER_NOTICE Report user-triggered notices
2047 E_ALL Report all errors and warnings
2048 E_STRICT Reports use of deprecated and unrecommended behavior; not included in E_ALL but very useful for code refactoring

When PHP is installed, there is a configuration file php.ini that the administrator can do some settings. To find where this file is located on your computer, run a phpinfo() script in your browser. You can edit the file to make changes to the settings:

You don't have to be notified about all of the different error categories. The error_reporting configuration directive controls which kinds of errors the PHP interpreter reports. The default value for error_reporting is E_ALL & ~E_NOTICE & ~E_STRICT, which tells the interpreter to report all errors except notices and strict notices.

If you don't have access to the php.ini, to change the error reporting level for the current script, you can call the function error_reporting(). A common way to use the function is like this:
// turn off error reporting
error_reporting(0);
// report everything
error_reporting(E_ALL);
// don't show notices
error_reporting(E_ALL & ~E_NOTICE);

Fixing Parse Errors

Parse errors happen when the PHP interpreter comes upon something unexpected in your program. If you leave out a necessary semicolon or start a string with a single quote but end it with a double quote, the interpreter doesn't run your program. It throws up its (virtual) hands, complains about a parse error, and leaves you stuck in the debugging wilderness.

This can be one of the most frustrating things about programming when you're getting started. Everything has to be phrased and punctuated just so in order for the PHP interpreter to accept it. One thing that helps this process along is writing your programs in an editor that is PHP-aware, such as BBEdit, Emacs, XEmacs, Komodo, Dreamweaver, PHPEd, PHPEdit, or Zend Studio.

These editors do syntax highlighting. This is a feature that changes the color of different parts of your program based on what those parts are. For example, strings are pink, keywords such as if and while are blue, comments are grey, and variables are black. Syntax highlighting makes it easier to detect things like a string that's missing its closing quote: the pink text continues past the line that the string is on, all the way to the end of the file (or to the next quote that appears later in the program).

Another feature of these editors is quote and bracket matching. This helps to make sure that your quotes and brackets are balanced. When you type a closing delimiter such as }, the editor highlights the opening { that it matches. Different editors do this in different ways, but typical methods are to flash the cursor at the location of the opening {, or bold the { } pair for a short time. This behavior is helpful for pairs of punctuation marks that go together: single and double quotes that delimit strings, parentheses, square brackets, and curly braces.

These editors also show the line numbers of your program files. When you get an error message from the PHP interpreter complaining about a parse error in line 35 in your program, you can focus on the right place to look for your error.

A list of all of the tokens that the PHP interpreter uses (and therefore that may show up in an error message) is in the PHP online manual at http://www.php.net/tokens.

When you get a parse error from the interpreter, first take a look at the line reported in the parse error. Check for the basics, such as making sure that you've got a semicolon at the end of the statement. If the line seems OK, work your way forward and back a few lines in the program to hunt down the actual error. Pay special attention to punctuation that goes in pairs: single or double quotes that delimit strings, parentheses in function calls or test expressions, square brackets in array elements, and curly braces in code blocks. Count that the number of opening punctuation marks (such as (, [, and {) matches the number of closing punctuation marks (such as ), ], and }).

Inspecting Program Data

Once you clear the parse error hurdle, you still may have some work to do before you reach the finish line. A program can be syntactically correct but logically flawed. Just as the sentence "The tugboat chewed apoplectically with six subtle buffaloes" is grammatically correct but meaningless nonsense, you can write a program that the PHP interpreter doesn't find any problems with, but doesn't do what you expect.

If your program is acting funny, add some checkpoints that display the values of variables. That way, you can see where the program's behavior diverges from your expectations.


Syntax | Basic Syntax | Text Strings | Here Document | Whitespace | Comments | Debugging Tips
© 2008: Hann So
email: hso@voyager.deanza.edu