PHP Debug Function





14
Date Submitted Mon. Oct. 9th, 2006 10:09 AM
Revision 1 of 1
Helper inxilpro
Tags debug | HTML | PHP | Variable
Comments 2 comments
Here's a basic function for debugging any kind of PHP variable. It allows for "invisible" or "visible" output--that is output in HTML comments or within visible HTML tags. It also supports customizable "containers" which lets you easily edit the look of the visible and invisible blocks. Examples in code below.

<?php
/**
 * Simple Debug Function
 *
 * @param mixed $var
 * @param boolean $visible
 * @param boolean $return
 * @return mixed
 */

function varDebug(&$var, $visible = true, $return = false)
{
        $containers = array(
                // $visible == false
                0 => array(
                        'head' => "\n<!--\n\nDEBUG:\n---------------------------------------\n\n",
                        'foot' => "\n\n---------------------------------------\n\n-->\n"
                ),
               
                // $visible == true
                1 => array(
                        'head' => '<hr /><h1>Debug</h1><p>',
                        'foot' => '</p><hr />'
                )
        );
       
        $r = var_export($var, true);
        if ($visible) $r = str_replace(array(' ', "\n"), array(' ', "<br />\n"), $r);
       
        $container = intval($visible);
        $r = $containers[$container]['head'] . $r . $containers[$container]['foot'];
       
        if ($return) return $r;
        echo $r;
}

/**
 * EXAMPLES:
 */


$a = array('h' => 1, 'e' => 2);

varDebug($a);

/*
OUTPUTS:
<hr /><h1>Debug</h1><p>array (<br />
  'h' => 1,<br />
  'e' => 2,<br />
)</p><hr />
*/


varDebug($a, false);

/*
OUTPUTS:
<!--

DEBUG:
---------------------------------------

array (
  'h' => 1,
  'e' => 2,
)

---------------------------------------

-->
*/


varDebug($a, false, true);

/*
OUTPUTS nothing
*/

?>
 

Chris M

Comments

Comments Useful . . .
Mon. Oct. 9th, 2006 11:30 AM    Scripter sehrgut
  Comments Re: Useful...
Mon. Oct. 9th, 2006 10:04 PM    Helper inxilpro

Voting