Tuesday, 23 January 2018

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;
        }
    }
}

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

debug($v);

0 comments:

Post a Comment