Power dump() on your PHP applications

Posted: 2010-09-28
Category: PHP

When trying to work out what the hell is going wrong in your PHP application the first thing most of us start doing is madly start var_dump()'ing everything possible to work out where the problem is. var_dump() is fine, but by default it comes out in one line. So then we need to echo <pre> tags. Then we can't always see whats going on, espeically if the background is black and bla bla bla so it goes on.

One option is to install XDebug which does basically the same thing, but sadly we don't all have access to SSH for our servers. Shared hosting, limited shell, can't install stuff, whatever. I spend a lot of time with CodeIgniter which is all about portability, make your stuff work anyway.

Using this handy function (which I have set up as a CodeIgntier debug_helper or native PHP debug_functions.php) I can dump any number of variables and always be able to see whats going on.


/**
* Debug Helper
*
* Outputs the given variable(s) with formatting and location
*
* @access public
* @param mixed variables to be output
*/
function dump()
{
list($callee) = debug_backtrace();
$arguments = func_get_args();
$total_arguments = count($arguments);

echo '<fieldset style="background: #fefefe !important; border:2px red solid; padding:5px">';
echo '<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @ line: '.$callee['line'].'</legend><pre>';
$i = 0;
foreach ($arguments as $argument)
{
echo '<br/><strong>Debug #'.(++$i).' of '.$total_arguments.'</strong>: ';
var_dump($argument);
}

echo "</pre>";
echo "</fieldset>";
}

Thanks to the fact this shows the line number and file it was called from I also manage to avoid those annoying "where the hell did I put that debug?!" situations, which is one of the main causes of swearing in my office.

Give it a try.

Comments

Gravatar
Chad

2010-09-29

This is ridiculously useful.

Could also have a global $dump to turn on/off output.

Has to be said, your defo under rated. Guess it's cos your not 15 ;)

Gravatar
Mini0n

2010-09-29

Wow. Awesome!
With this and the IP validation for the debug to show only to my IP address, it'll make my life easier.

Also, shouldn't there be a counter inside the foreach, to make "Debug #" dynamic?
As it is, it will always be "Debug #1", right?

Thanks for this script!

Gravatar
Manuel Silva

2010-09-29

Hi Phill,

good function this one, i've just tweak it a litle bit:

if (is_array($argument))
{
print_r($argument);
}
else
{
var_dump($argument);
}

print_r gives a prettier output. :P

Gravatar

2010-09-29

Or, if you can install Xdebug, you will have a prettier var_dump().

Gravatar
Yrizos

2010-09-29

Hey, that's nice.

debug_backtrace() also returns the arguments, so you can replace

$arguments = func_get_args();

with

$arguments = $callee['args'];



Gravatar
Rob

2010-09-30

Another debug tip... Don't echo the content to the browser at all. Use the error_log command instead. Then you can see what's going on by tailing the apache error log. This is particularly useful for requests that come in via ajax.

Gravatar
Jc

2010-09-30

Hi Mini0n,

Take a look at the code again. the variable $i is instantiated before the loop with a value of 0. Then that variable is incremented by 1 before it is echo()'ed out. That is what ++$i does. it increments the variable before the value is returned. On the other hand $i++ it returns the value and then increments it.

Good Luck

Gravatar
George Secrieru

2010-10-01

Very good idea for a quick debugging solution!

Regards,
George

Gravatar
Matthew Fedak

2010-10-01

Nice handy little function there thanks phil, is this anywhere in the wiki yet or on the forums?

Gravatar
Mini0n

2010-10-01

Hi Jc,

When I made my reply, I think there was no $i initialization nor the ++$i.

Gravatar
Lucas

2010-10-04

Better than my
var_dumb($var) {
echo '';
var_dump($var);
echo ''
}

:P

Gravatar
Shin1x1

2010-10-05

Or, if you can install Xdebug, throw new Exception;

Gravatar
Moosh

2010-10-05

I have same function

For the legend I call another :

/**
* return a comptact backtrace output
*
* @return string
*/
function backtrace_inline ()
{
$arrBt = debug_backtrace();
//@see http://php.net/manual/fr/function.debug-backtrace.php
$strBts = '';
if (is_array($arrBt)) {
unset($arrBt[0]);
$fn = null;
foreach ($arrBt as $bt_line) {
if (is_array($bt_line))
if (is_array($bt_line['args'])) {
$args = str_replace( "\n",' ', str_replace( "\r",'', var_export($bt_line['args'], 1) ) );
}
$strBts .= ($fn !=
basename($bt_line['file'])) ? 'in [' .
basename($bt_line['file']) .
'@' .
$bt_line['line'] .
']' : '';
$strBts .= ' for ' .
$bt_line['class'] .
$bt_line['type'] .
$bt_line['function'] .
'( ' .
$args .
' ) ';
$fn = basename($bt_line['file']);
}
}

return $strBts;

}



Note I've also added code to don't output the debug if I detect I'm on Prod Statging, and I send a mail to manager if it's happen.


If the variable is an array of array I user PEAR::HTML_Table instead of var_dump.

Gravatar
Dalibor Simacek

2010-10-05

Hey, personally I like the dump function used in Nette Framework (nette.org). Since I still need to use CI sometimes, I extracted the code and all you need to do is to put it to some helper just like yours.

Here's the code: http://pastebin.com/H6Jv9Thq

Gravatar
Stephen Holsinger

2010-10-08

Or you could just use: log_message('DEBUG', 'class::function - '. print_r($var, TRUE) ); then look through your logs. (when using CodeIgniter)

Gravatar
Luke Holder

2010-10-09

simply the BEST dump script!

http://dbug.ospinto.com/

Posting comments after three months has been disabled.