PHP Debugging Techniques

When it comes to debugging, what you’ll best learn from experience are the causes of certain types of errors. Understanding the common causes will shorten the time it takes to fix errors.

Common PHP Errors
  • Blank Page : HTML problem, or PHP error and display_errors or error_reporting is off.
  • Parse error : Missing semicolon; unbalanced curly braces, parentheses, or quotation marks; or use of an unescaped quotation mark in a string.
  • Empty variable value : Forgot the initial $, misspelled or miscapitalized the variable name, or inappropriate variable scope (with functions).
  • Undefined variable : Reference made to a variable before it is given a value or an empty variable value (see those potential causes).

  • Call to undefined function : Misspelled function name, PHP is not configured to use that function (like a MySQL function), or document that contains the function definition was not included.
  • Cannot redeclare function : Two definitions of your own function exist; check within included files.

  • Headers already sent : White space exists in the script before the PHP tags, data has already been printed, or a file has been included.
How to aviod syntactical Errors
  • End every statement (but not language constructs like loops and conditionals) with a semicolon.
  • Balance all quotation marks, parentheses, curly braces, and square brackets (each opening character must be closed).
  • Be consistent with your quotation marks (single quotes can be closed only with single quotes and double quotes with double quotes).
  • Escape, using the backslash, all single and double-quotation marks within strings, as appropriate.
To debug your scripts
  • Turn on display_errors.
  • Use the print() and echo() functions.
  • Check what quotation marks are being used for printing variables.
  • Use comments.
  • Print array values using print_r() and var_dump().



June 6, 2008 at 11:44 AM

"Use the print() and echo() functions."

print and echo are not functions...

  Suresh Kumar A

June 6, 2008 at 7:44 PM

I appreciate your valuable comment. echo() and print() are built in functions used to print the output in web browser

  Daniel Neville

June 7, 2008 at 2:21 AM

TIM is 100% correct. print and echo are not functions, they are language constructs. This means they do not have the overhead normally associated with making a call to the functions lookup table.

The only difference between echo and print is print has a return value (int 1, always) whereas echo does not.