Debug function

A simple but useful debug function which outputs a variable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * 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($vartrue));
        } 
        elseif (is_string($var)) {
            echo "string(" . strlen($var) . ") \"" . htmlentities($var) . "\"\n";
        } 
        else {
            var_dump($var);
        }
        echo "</pre>";
 
        if ($exit) {
            exit;
        }
    }
}
//Example:
$v = array(1,2,3,array(1,2,3),array('a' => 'foo''b' => 'bar'));
 
debug($v);
X

Url: http://www.jonasjohn.de/snippets/php/debug-function.htm

Language: PHP | User: ShareMySnippets | Created: Oct 16, 2013