StatsBurner v2.3

This PHP class is a wrapper for the Feedburner Awareness API. It grabs your circulation hits, views, stats and averages over a given period of time. Can be customized to grab data from certain dates, date ranges and discrete dates.

Recent Thoughts

Creating a simple debug() function

Saturday, May 30th 2009, 1:15am
Topics: Programming, PHP & MySQL
Tags: Debug, Function
Views: 1,464, Comments: 5

If you are ever like me and hate having to type print_r and echo pre blocks whenever you want to debug a variable or array, this function is for you! This is a global function that can be used to output any data that you need to debug and it comes equipped with some nifty features. For one thing, the debug() will not output any data on the website if error_reporting is turned off, which is good for instances where you forget to remove the debug() code! It will also display the file and line number where the debug() was called, and you can pass true as the second argument to do a var_dump instead of print_r. Here's the code for your enjoyment!

/**
 * Outputs/Debugs a variable and shows where it was called from
 * @param mixed $var
 * @param boolean $dump
 * @param boolean $backtrace
 * @return string
 */
function debug($var, $dump = false, $backtrace = true) {
	if (error_reporting() > 0) {
		if ($backtrace) {
			$calledFrom = debug_backtrace();
			echo '<strong>' . trim(str_replace($_SERVER['DOCUMENT_ROOT'], '', $calledFrom[0]['file'])) . '</strong> (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
		}
		
		echo '<pre class="debug">';
		$function = ($dump) ? 'var_dump' : 'print_r';
		$function($var);
		echo '</pre>';
	}
}


Please note that this is a very basic debug function with very basic backtrace functionality. Here are a few examples on how to use the function (if for some reason you don't understand it).

debug($_SERVER);

// var_dump() instead of print_r()
debug($_SERVER, true);

// Do not display the backtrace
debug($_SERVER, false, false);
Related Entries:

5 Comments

10 / 2 = ?
Allowed: [code] [b] [i] [u]
  • Dan
    code621.com
    May 31st 2009, 00:32
    1 Hmm... I think Cake's existing debug function (http://book.cakephp.org/view/699/debug) is pretty useful but no harm in creating one that works exactly how you like.
  • Miles Johnson
    www.milesj.me
    May 31st 2009, 02:00
    2 @Dan - This is meant for non-CakePHP apps.
  • Dan
    code621.com
    May 31st 2009, 08:47
    3 Sorry Miles, I don't know why I assumed you were talking about CakePHP. Good work.
  • Arnoud ten Hoedt
    twitter.com/roonaan
    Jun 16th 2009, 00:43
    4 Good work,

    As a debug concept, I introduced on top of this kind of debug calls the usage of commented markers:

    /** **/
    Debug php code
    /**
    **/

    Then as part of the building and deployment to Test/Acceptance/Production this sections would be removed, not causing any load or unnecessairy logical testing knowing that you aren't debugging anyway on your production env.

    Personally I use ANT, but you could use phing or maven if you like.
  • Ramiro Varandas Jr
    Jun 16th 2009, 06:06
    5 You can also use XDebug to better improve the var_dump() output and use it with Netbeans or Eclipse to debug your code with breakpoints.

    With Netbeans I have used it, but not with Eclipse.