5 important tips for Debugging PHP Code

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.

12 thoughts on “5 important tips for Debugging PHP Code”

  1. 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 “”;
    }
    ?>

  2. 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.

Leave a Reply