Enhancing Websites with PHP

Troubleshooting & Debugging

The desire of all web designers and programmers is to write perfect code the first time every time. Unfortunately, that rarely happens especially if you are just starting to learn a language. As you gain more experience, you will learn to not only expect errors but to embrace them as you discover how rewarding it is to be able to fix errors yourself and how much more you understand each time you do.

The key to building your troubleshooting skills is to remember the following:

  1. If you can read and interpret the code you’ve written, you can better understand resulting error messages and understand why things may not appear as expected;
  2. If you can understand an error message, you can more readily back-trace it to within a few lines of code. Note that in an interpreted language, errors are caused on or above the line referenced in the error message.
  3. If you know approximately where the problem exists, you can more readily isolate it and fix it.

With respect to more complex error conditions, it may also be necessary to attempt to replicate the problem in order to solve it, but more on that level of troubleshooting later in the course.

For now, know that errors or unexpected output can be caused either by your XHTML components or by your PHP script. Common XHTML problems include:

  1. Page not found (page doesn’t exist, servers are not running, the path to the file is invalid, or the page is not being accessed via the browser through localhost);
  2. CSS formatting does not get applied (CSS file does not exist or CSS link contains an invalid path to the file);
  3. Images don’t appear (the image doesn’t exist or the image tag contains an invalid path to the file).
  4. Form data does not get processed by the script (missing or invalid action or method or missing or invalid form element name values).
  5. Other possible issues that cause unexpected output are omitting closing tags, improper nesting of tags, missing or incorrect characters or punctuation, misspelling tag names.

To troubleshoot PHP code errors, it helps if you can first identify if the error is:

  1. Syntactical - it may be missing characters or punctuation, such as a semi-colon, it may be using mismatched, missing, or incorrect braces or brackets, or it may reference invalid variable names that do not allow passing a value from the form to the script;
  2. Logical - the code is sequenced incorrectly, such as when it attempts to make a decision before the conditions of the decision are known;
  3. Runtime-related - the server or client is in a state that prevents execution, such as when a connection is lost.

At this stage, you will be exposed to common XHTML issues (many of which you may already be familiar with) and common PHP syntactical and logical mistakes beginners tend to make. You will then learn basic techniques for isolating and fixing them. You’ll learn more about runtime errors later when you learn about databases.

To help you maintain a healthy perspective about errors, remember “seeing” your code as right even if it isn’t is normal and happens repeatedly when your eyes are fatigued. Sometimes all it takes is walking away and taking a break. In addition, making arbitrary changes to your code without first understanding what component is causing it or what an error is telling you is usually non-productive and results in frustration and diminishing confidence in your ability. Try to avoid that situation and instead seek the appropriate assistance.