Quick and dirty PHP debugging

Xdebug (http://xdebug.org) is probably one of the best ways to debug PHP code. But Xdebug can be difficult to setup, depending on the environment.

If you want an one-liner to see defined variables, try this :


print print_r(array_keys(get_defined_vars()), 1);

If you only want to see a select list of PHP variables, try this which was lifted from the notes at http://php.net/manual/en/function.get-defined-vars.php.


    function getDefinedVars($varList, $excludeList) {
        $temp1 = array_values(array_diff(array_keys($varList), $excludeList));
        $temp2 = array();
        while (list($key, $value) = each($temp1)) {
            global $$value;
            $temp2[$value] = $$value;
        }
        return $temp2;
    }
    
    $excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 
       'HTTP_COOKIE_VARS', 'HTTP_SERVER_VARS', '_SERVER', 'HTTP_ENV_VARS',
       '_ENV', 'HTTP_POST_FILES', 'HTTP_SESSION_VARS', 'excludeList');

    print_r(getDefinedVars(get_defined_vars(), $excludeList));

Categories: