var_dump enhancement?

Has someone written a replacement, or enhancement, of var_dump? I am using xdebug’s version, but is anything else available?

Here is one I wrote earlier :slight_smile:

http://johns-jokes.com/downloads/_fred.txt

It works with boolean, strings, arrays, object, etc



# Usage
 require '_fred.php';

$a=array
(
  'Tom',
  'Dick',
  'Harry',
);

fred( $a , 'optional title goes here');

# output:
Name:  optional title goes here

Type ==> array(3)
Count  ==> 3
Value  ==>
Array
(
    [0] => Tom
    [1] => Dick
    [2] => Harry
)



I have been using the Zend Framework 1 Debug class for many years. You can pull it right out of the framework with out any difficulty. It does a good job of formatting things and seems to handle circular reference in a rational fashion.

What else do you want from an improved var_dump()?

This supports both cli and web.



/* ------------------------------
 * Basically copied from an old version of the Zend Framework 1
 * The debug routine is very handy
 */
namespace Zayso\\CoreBundle\\Component;

class Debug
{

    /**
     * @var string
     */
    protected static $_sapi = null;

    /**
     * Get the current value of the debug output environment.
     * This defaults to the value of PHP_SAPI.
     *
     * @return string;
     */
    public static function getSapi()
    {
        if (self::$_sapi === null) {
            self::$_sapi = PHP_SAPI;
        }
        return self::$_sapi;
    }

    /**
     * Set the debug ouput environment.
     * Setting a value of null causes Zend_Debug to use PHP_SAPI.
     *
     * @param string $sapi
     * @return void;
     */
    public static function setSapi($sapi)
    {
        self::$_sapi = $sapi;
    }

    /**
     * Debug helper function.  This is a wrapper for var_dump() that adds
     * the <pre /> tags, cleans up newlines and indents, and runs
     * htmlentities() before output.
     *
     * @param  mixed  $var   The variable to dump.
     * @param  string $label OPTIONAL Label to prepend to output.
     * @param  bool   $echo  OPTIONAL Echo output if true.
     * @return string
     */
    public static function dump($var, $label=null, $echo=true)
    {
        // format the label
        $label = ($label===null) ? '' : rtrim($label) . ' ';

        // var_dump the variable into a buffer and keep the output
        ob_start();
        var_dump($var);
        $output = ob_get_clean();

        // neaten the newlines and indents
        $output = preg_replace("/\\]\\=\\>\
(\\s+)/m", "] => ", $output);
        if (self::getSapi() == 'cli') {
            $output = PHP_EOL . $label
                    . PHP_EOL . $output
                    . PHP_EOL;
        } else {
            $output = '<pre>'
                    . $label
                    . htmlspecialchars($output, ENT_QUOTES)
                    . '</pre>';
        }

        if ($echo) {
            echo($output);
        }
        return $output;
    }

}

Thanks a lot for the replies and example code. Its given me a lot to think about. I had done some of the things John Betong did in his code, but didn’t nearly take it nearly as far or as comprehensively. I haven’t used Zend, so I will take a look at it that, ahundiak.