Debug PHP: print_r() the array

I’ve used print_r method in PHP to print the contents of an array or object for a long time. I know there are some debugging tools out there, but I like it real simple, and print_r is really all I have needed.

Well, the mistake I’ve done is that I write this snippet all over my code:
echo '<pre>';
print_r($array);
echo '</pre>';

The work in removing all them or commenting out is something I sometimes forget. That is why I made a new kind of method to make it easier.
function preWrap($array){
echo '<pre>';
print_r($array);
echo '</pre>';
}

I don’t know if this is a really good way of doing it, but it helps. To do it so that removing the preWrap() call, or commenting it out, is not always needed, you can add this to it:

In top of index.php or config.php file:
$debug=true;//change to false to turn of debug mode

And then in the preWrap method add this:
if($debug){
echo '<pre>';
print_r($array);
echo '</pre>';
}

That’s my simple way of doing it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: