PHP debugging

php-debug.jpgManaging PHP debugging can be tricky as, by default, errors and test ‘echo’ statements can be lost along with all the other output. This problem is compounded when using AJAX, and errors can turn up in XML streams, causing all sorts of problems.

A better solution is to redirect all of that output into an external file, and use a tool like tail to monitor the log.

For printing debug statements, a debug function that shows where each statement came from (file and line number), and is capable of printing complex objects is useful too.

The following is the debugging code that’s included in SiteComponents.

if ( defined("DEBUG_ENABLED") )
   ini_set("error_log", $_SERVER["DOCUMENT_ROOT"]."/debug.log");
   ini_set("error_reporting", E_ALL);
   ini_set("display_errors", false);
   ini_set("log_errors", true);

/** * Define debugging routine */ function SCDEBUG($content="") { if ( !defined("DEBUG_ENABLED") ) return;

if ( !array_key_exists("__SCDEBUG_fd", $GLOBALS) )
   $GLOBALS["__SCDEBUG_fd"] = fopen($_SERVER["DOCUMENT_ROOT"]."/debug.log", "a");
    if ( !$GLOBALS["__SCDEBUG_fd"] ) return;

$bt = debug_backtrace();

if ( is_array($content) || is_object($content) )
   $content = print_r($content, true);

      ($bt[0]["file"] ? (substr($bt[0]["file"], 
            strlen($_SERVER["DOCUMENT_ROOT"])+1).":".$bt[0]["line"]." (") : "").
        ": ".$content."\n");


, . Bookmark the permalink. Both comments and trackbacks are currently closed.