5 important tips for Debugging PHP Code


by Php Ninza on August 29, 2009

Debugging PHP code is a  nightmare for all Php developers and these are the times when they miss the thread functionality like java in php, However some simple php debugging techniques can help y0u to code faster and thus save very valuable coding time.

These all debugging techniques are based on my experience and i would love to know what more techniques php programmers use for debugging there php code !, Please comment below and share with community if you know a secret weapon in php code debugging.

So , My 5 debugging tips are (in no particular order):-

1) Live PHP debugging:- Use a editor with inbuilt live Php debug (Like phped ,Phpdesigner, eclipse etc), Editors like editpad, notepad doesn’t support live debugging and thus need to run the php file on browser for finding the errors. These editors run php scripts via command line on every save and present any errors found.

Small human errors like missing a semicolon , not terminating the line correctly, missing brackets etc can be easily found using a live debug php editor.

2) Use Xdebug:-The Xdebug extension allows you to find  stack traces and function traces in error messages, memory allocation and protect from infinite recursions happening

It also provides profiling information for PHP code and is having the capability to debug scripts interactively with a debug client.

Xdebug can be found at xdebug.org (Editors like Phpdesigner and phpdebug already contain Xdebug inbuilt )

3) Activate Php error messages:- php.ini contains a lot of configuration options , Among those options there are a couple of options which control the way error messages are displayed to a user. Make sure that following two options are set as follows:-

display_errors = On
error_reporting = E_ALL & ~E_NOTICE

4) Use print and echo statements beforehand at critical points for debug:- This is somewhat a homegrown method that i use for php debugging which i found extremely helpful and useful in debugging big php codes.

While coding i create some virtual sections of code and in these virtual sections i put the echo statement to ensure that the code is passing through that portion correctly and echoing the data correctly that’s required.

But since i can’t left them always on , I put up a debug_check if statement to check whether the code is working in debug mode or production mode.

For example

< ?php

$debug_check = 1;

foreach ($array as $data) {

    if ($debug_check == 1)

        print ($data);

}

?>

Here the $debug_check variable is defined at the top of the web script. Once this is changed to 1, it displays all echo and print statements , This is when i am debugging the code.

When i am in production mode i simply change the value of $debug_check to 0 which again hide all echo and print statements,  This takes a little more time in coding initally , but proves to very helpful in long run.

5) Use Frameworks:- Frameworks are one of the most important change that is implemented in modern programming , Php frameworks like codeignitor and cakephp provides a lot of functionality for setting up test cases, units and debugging.

Also, Since most of the code is ran via libraries the error messages are nicely crafted and provides inbuilt details.

Two most favourite frameworks of mine are:- Cakephp and Codeignitor

Hope everyone enjoyed thsese 5 debugging techniques in PHP , I request everyone to share there piece of advise in php debugging , Will be compiling all of them in a a blog post with proper credits and publishing it !, It might proves useful to every php developer around the world.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 11 comments… read them below or Shout @ me! }

phpcollection August 29, 2009 at 7:02 pm

New Blog Post:- 5 important tips for Debugging PHP Code http://bit.ly/19MUAN

This comment was originally posted on Twitter

gmishra August 30, 2009 at 5:29 am

RT @phpcollection: 5 important tips for Debugging PHP Code http://bit.ly/19MUAN

This comment was originally posted on Twitter

Joseph August 31, 2009 at 12:23 pm

These are some really good tips. The one thing I would add though is in relation to number 4. There really needs to be a third level of debugging for when the site is live. The debug data can then be commented out into the HTML instead. For example:

= 1) {
if ($debug_check == 1) print “”;
}
?>

Ricardo Soares de Lima September 24, 2009 at 2:09 pm

Nice tips! One tool that I think is really valuable is FirePHP (http://www.firephp.org/), it’s better than using echo statements to show values.
And if you don’t mind, I’ll translate your article to portuguese in my blog, crediting your article off course.

Php Ninza September 24, 2009 at 4:06 pm

@Ricardo

Sure you can , Also if you can link up some articles on firephp as well that would be very useful to our readers

Ricardo Soares de Lima September 24, 2009 at 11:18 pm

Thanks Ninza! This is a great article to start with firephp (http://sixrevisions.com/web-development/how-to-debug-php-using-firefox-with-firephp/). Until the weekend I’ll get your article translated.

Ricardo Soares de Lima September 26, 2009 at 9:46 pm

Hey! Article translated, here is the link:
http://hipercodigo.blogspot.com/2009/09/traducao-5-importantes-dicas-para.html

Thanks!

Php Ninza September 27, 2009 at 4:40 am

Thanks Ricardo,

Appreciate the effort

SeanJA May 16, 2010 at 2:14 am

Well… good except to future proof your application, you should be coding to:
E_ALL | E_STRICT

Ivo Roper May 28, 2010 at 4:08 am

If you have a complex project going, it’s helpful to extend item #4 to use a range of $debug_level values (e.g. 0 to 3) so that you can include a boatload of debug messages when you need to see everything at level 3, and use level 1 debug messages when you want to see only a few specific items of your work in progress.

You can also use a global scope function like debug_print( 2, “$foo is dead”) instead of a variable, so you can access it within class scope without global declarations. The current level can be stored however you want, and only the debug function needs to know how and where.

I’ve also thought about merging my logging and debug print functionality, but I don’t need that currently / yet.

saurav June 24, 2010 at 4:26 pm

s

Leave a Comment

Previous post:

Next post: