PHP dBug – better debugging

When your knee deep in code, you need tools that will find your mistakes quickly, and give you needed information to troubleshoot why your code is not doing what you expected.

One of the things you want to know is if variables are being passed between files correctly, since the Internet is stateless, and each new page doesn't remember what you've done in previous page. I wrote a script that addressed this problem, and talked about how to use it in another post entitled, PHP - Code Snippet - a Quick Variable Checker.   This utility specifically addressed what Cookies, Session, Get, and Post variables were available to you when you opened a new PHP  file.

But you want more information when your in the middle of the code in a page, specifically what a variables value is at that spot in the code.

PHP had addressed this issue with several commands.  The most common way to find the value out is to "echo" the command.  The way you usually do that is to give it some introductory text so you know what you are echoing, something like:

$projname = "Tecumseh";
echo "project name is: " . $projname;
and the output: "project  name is: Tecumseh"

This at first seems adequate, but there is a problem.  It will not echo an array.
If you echo an array, it simple says "Array," or too bad for you.

For arrays we are directed toward "print_r."  Print_r will print out a readable array or object, for example, let's make an array:

$yum = array("apples", "french fries", "candy");

And the output looks like this:

You may want to wrap the output in "pre" tags to make the print out look pretty.  Something like this,

echo "<pre>";
echo "</pre>";

which outputs like this:

The "pre" tags can get to be a bit of a pain to echo out every time you want to look at a variable.

Then there is var_dump, which is used the same as print_r.  The outputs not as pretty, but it gives you a little more information, like variable length.

Here's var_dump($yum):

You can also make this pretty with "pre" tags, like with print_r command, and the output looks like this:

Then we come to objects. For this example, I'll use an object  I'm using at work to show you the various outputs.  With this object instance, I am getting ready to save the instance of the object into the database.   Echo, surprisingly, will output the values in the object.
Like so:

echo $EdtReqSav;

Gives you this result:

Print_r looks like this:


and the output comes out:

var_dump looks like this:


and outputs like so:

This is all well and good, but I'm getting a headache from all the extra typing and trying to decide how I want my output.  There is another way...

Enter a new tool for you PHP debuggers, called dBug appropriately enough.  PHP dBug has been around for awhile, the last update being Dec, 2007, but that doesn't limit its usefulness, and I find I use it every day.  That's right, every day.  I've grown to love it and trust it.

PHP dBug is a free utility that can be downloaded here.   It's simple to use.
You include the dBug class code in your code at the top of  your file, like so:


Once the class is included in your file, to use dBug on any variable, string, array, object, database resource, or XML resource, that's right with any variable no matter what the type,  you use the same easy technique.

new dbug($variable or $object);

That's it, easy typing, one command for every thing, and just look at the output from our previous examples.

Here's a variable from "new dbug():

new dbug($projname);

This yields simply "Tecumseh," as before.

Let's try an array using

new dbug($yum);

and the output looks like this:

Here's an object, you'll notice that object methods are displayed, which you don't get in print_r and var_dump.

new dbug($EdtReqSav);

The output is:

I get all the methods, which echo, print_r, and var_dump will not give you,  a vast improvement.   The variable values are not listed in this example.  If we used dbug on the class, we would also get those.

I find I like having the methods.  I use them as guides when asking for variable values in the object.  If I want the values, I can always revert back to echo, or print_r, or var_dump, but in reality I find the methods are all I need, if I get the methods I know my object is instantiated.

To see more examples of the superb output of dbug go to their web site where they have additional screen shots and documentation.  I've found I have stopped using echo, print_r, and var_dump in favor of the dbug() class.  Give it a try.

Comments are closed.