April 16, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Debugging PHP

  • September 30, 2002
  • By Elizabeth Fulghum
  • Send Email »
  • More Articles »

If you have ever written your own scripts in PHP or another programming language, you know the frustration that comes from debugging your work.

Write an HTML page and leave off a closing </table> tag, or even something vital like the <body> tag, and most of the browsers in use today will display the page as you intended, even with the errors. But programming languages in general - and PHP specifically - are not nearly as forgiving; leaving off even a single semi-colon can stop the execution of the entire script.

In this article, you'll learn more about programming errors and the types of core error handling features PHP offers. You'll also learn a few tips and tricks for troubleshooting common errors in your own scripts.


Types of Errors

Generally speaking, there are two types of errors in programming: Syntax Errors and Logical Errors.

A syntax error is a lot like a grammatical error in a sentence; something that you have written (or omitted) has broken the rules of the language. Common mistakes such as missing semi-colons, mismatched or un-escaped quotes are all good examples. Because a statement is either formed correctly or incorrectly, syntax errors are objective, and the easiest to troubleshoot.

Logical errors, on the other hand, are far more insidious. These occur when the flow of the application has been incorrectly conceived or programmed. It is easy to see when one exists because the script does not produce the desired results, but hard to isolate and fix because no error messages are produced (at least not directly).


Preventing and Resolving Errors

Prevention of logical errors should begin before a single line of code is written, especially on larger projects that have more contingent programming. By outlining each of your scripts using pseudo code, much like you might outline a speech or essay you were planning to write, you can develop a framework to follow when you begin scripting.

For example, if you were writing a script that accepts input from a user in a form and then emails the information, the pseudo code might look something like this:

If the form has been submitted, then
   Check to see if all the fields are filled out. If they are, then
      Email the form contents
      Display a thank you page
      Exit the script
   If they aren't
      Create an error message

Display the form

As you can see, though written in such a way that even a layman could follow, this pseudo code bares strong resemblance to a standard PHP if statement.

Because a framework like this addresses all the logical aspects of the script's program flow in advance, following it during the programming process helps you stay on track and naturally minimizes logical errors. When you become more familiar with the logical apsects of programming, you can rely less on an outline process in advance. For large projects, it typically becomes necessary only to outline which scripts you plan to write, and what their functions should be.

Unlike logical errors, syntax errors seem to stubbornly defy prevention, even for the most experienced programmer. They pop up as typos, misnamed variables and accidental omissions. The trick to handling them is being able to quickly isolate and fix them. Generally, this is easy to do because PHP terminates with an error message citing the line number and type of error; most times it's just a matter of knowing what to look for. Below are a few common PHP error messages:

General Parse Errors

Examples:
Parse error: parse error, unexpected T_... in /home/httpd/www/index.php on line 3
Parse error
: parse error, unexpected T_... in /home/httpd/www/index.php on line 4

Parse errors usually offer little clue to the exact problem (although error messages in the latest versions of PHP are becoming more specific), but do give you the correct line number to look at most times. Check for missing semi-colons, quotes and parentheses as well as mismatched quotes on the line specified in the error, as well as those directly before it.

If the parse error message references the last line of the file or a line where there is no PHP, it usually means that you have forgotten a closing bracket ( }) on a loop or if-else statement.

Header Errors

Example:
Warning: Cannot send . - headers already sent (output started at /home/httpd/www/index.php:2) in /home/httpd/www/index.php on line 5

This type of error occurs when you attempt to send header information after you have already outputted information to the browser through PHP or with HTML. This applies not only to the header() function, but also to cookie and session functions.

Paremeter Count Errors (functions)

Example:
Warning: Wrong parameter count for explode() in /home/httpd/www/index.php on line 5

Unless specifically set up to accept a variable number of arguments, functions in PHP require that the number of arguments being passed to them match the number specificed in the function declaration. Especially in PHP's pre-defined functions, be sure that you have not left off or added an additional argument.






Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel