1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Check Your PHP

How to check over your PHP for errors!


There is nothing more frustrating than spending hours working on a bit of code, only to have it do nothing. Often there is something very simply causing big problems. Here are some handy tips for troubleshooting problem code.

1. Use a color coded editor

I'm not a big fan of PHP editors that do too much of the coding themselves. Actually, I love simple plain text editors. But the one "fancy" thing I guess I do appreciate sometimes when I'm having a PHP issue is an editor that color codes my PHP. This is an easy way to see if something never ends. You'll sure notice that missing bracket when the entire bottom half of your code is one color!

2. Use Comments

And I don't mean use comments in a "This code does this" sense.  Not that you shouldn't do that... but what I'm referring too is commenting out large sections of your code at a time to try to better isolate the problem.  At least if you can figure out vaguely were the problem is occuring, you have a better chance of finding and fixing it.  

3. Test, Test, Test

I know by the time you finish and run into an error, this might be too late... but its good to keep in mind when writing the code in the first place. It's good to keep testing your code often. Every time I add something new, I just give it a quick test to make sure everything is still working as planned. This way if something does go wrong, I know it's probably a problem with my newest addition to the code.

4. A Fresh Set of Eyes

I know it sounds stupid, but if you walk away form your keyboard, have a soda, just give it ten minutes, sometimes you see things you missed after staring at the same lines for hours. Take a break, it might help!

5. Error Reporting

If you are running into PHP errors, you probably want to make sure you have error reporting enabled. This takes a lot of the guess work out of what is going wrong, so you know where to start fixing!
Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Troubleshooting
  5. Check Your PHP for Errors

©2014 About.com. All rights reserved.