Simple var pretty debugger

Home   »   Simple var pretty debugger

/**
 * Simple var pretty debugger
 * 
 * @param $var
 * @param false $exit
 */
function pre_dump($var, bool $exit = false) {

    //TODO: Only works if there is a pre_dump() call per line
    $backtrace = debug_backtrace();
    $file      = file( $backtrace[0]['file'] );
    $line      = $backtrace[0]['line'] - 1;
    preg_match( '#\((.*?)\)#', $file[ $line ], $match );
    $var_name = trim( $match[1], '()' );

    echo '
';

    if ( is_array( $var ) ) {

        echo "+ var_name (array) => " . $var_name . "):\n";
        echo "Called from " . $backtrace[0]['file'] . ' on line ' . $backtrace[0]['line'] . "\n\n";
//			var_dump( $var );
        print_r($var);

    } elseif ( is_object($var) ) {

        echo "+ var_name (object) => " . $var_name . "):\n";
        echo "Called from " . $backtrace[0]['file'] . ' on line ' . $backtrace[0]['line'] . "\n\n";
        print_r( $var );

    } elseif (is_string($var)) {

        $string = json_decode($var, false);

        //normal string
        if ( json_last_error() !== JSON_ERROR_NONE ) {

            echo "+ var_name (string) => " . $var_name . ":\n";
            echo "Called from " . $backtrace[0]['file'] . ' on line ' . $backtrace[0]['line'] . "\n\n";
            echo 'string(' . strlen( $var ) . ') "' . htmlentities( $var ) . "\"\n";

            //json string (will return the json_decode output as object)
        } else {

            echo "+ var_name (json_decode output) => " . $var_name . ":\n";
            echo "Called from " . $backtrace[0]['file'] . ' on line ' . $backtrace[0]['line'] . "\n\n";
            print_r($string);

        }

    } else {

        echo "+ var_name => " . $var_name . ":\n";
        echo "Called from " . $backtrace[0]['file'] . ' on line ' . $backtrace[0]['line'] . "\n\n";
        var_dump($var);

    }

    echo "\n

";

if ($exit) {
exit;
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *