Return to Snippet

Revision: 30095
at August 9, 2010 21:04 by Sverri


Updated Code
if ( ! function_exists('debug'))
{
  function debug($var, $fn='print_r')
  {
    if (function_exists($fn))
    {
      echo '<pre>', $fn($var), '</pre>';
    }
    else
    {
      echo '<pre>The function ', $fn, '() does not exist. Falling back to',
      ' print_r().<br /><br />', print_r($var), '</pre>';
    }
  }
}
 
// Example
 
debug(array('test1',123));
 
/*
<pre>Array
(
    [0] => test1
    [1] => 123
)
1</pre>
*/

Revision: 30094
at August 7, 2010 10:06 by Sverri


Initial Code
function debug(&$var, $fn='print_r')
{
  echo '<pre>', $fn($var), '</pre>';
}

// Example use

$obj->one = 1;
$obj->two = "two";
$obj->three = array(1, 2, 3);

debug($obj);

/*
stdClass Object
(
    [one] => 1
    [two] => two
    [three] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)
*/

Initial URL

                                

Initial Description
When working in PHP I sometimes want to know exactly what is in a variable. This is easy enough to find out, but having this function around it is even easier, as it includes the HTML pre tag making it easier to read, and it allows you to choose how it should be debugged (defaults to using print_r()).

Initial Title
Debug a variable

Initial Tags
debug

Initial Language
PHP