Debug function

A simple but useful debug function which outputs a variable

/**
 * void debug ( mixed Var [, bool Exit] )
 *
 * Carlos Reche
 * Jan 14, 2006
 */
 
if (!function_exists("debug")) {
 
    function debug($var, $exit = false) {
        echo "\n<pre>";
 
        if (is_array($var) || is_object($var)) {
            echo htmlentities(print_r($var, true));
        } 
        elseif (is_string($var)) {
            echo "string(" . strlen($var) . ") \"" . htmlentities($var) . "\"\n";
        } 
        else {
            var_dump($var);
        }
        echo "</pre>";
 
        if ($exit) {
            exit;
        }
    }
}
Snippet Details
  • Author
    Carlos Reche (found in an php.net comment)
  • License
    ?
  • Language
    PHP
  • Created
    04/05/2006
  • Updated
    04/05/2006
  • Tags



$v = array(1,2,3,array(1,2,3),array('a' => 'foo', 'b' => 'bar'));
 
debug($v);

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.