Colored var_dump() and errors
Asked Answered
L

6

8

How can I set style to var_dump() function and PHP errors style, like on the next image?

enter image description here

enter image description here

At the moment I have next view of var_dump() (with <pre>var_dump(...)</pre>, without it will be all in one line) and just plain text for errors.

enter image description here

I searched something for PHP colored errors, var_dump styles, but could not find anything.

I used OpenServer as localhost, and on previous version I had same styles for errors, but now just plain text. Is it real to customize?

Litharge answered 2/8, 2015 at 12:20 Comment(4)
Google: PHP XDebug First result.Killebrew
Hello Rizier123! Yea, i googled something about it, and found that it's already enabled in my localhost - php.ini. If i'll make settings my IDE for them, it somehow affect on my browser style output?Litharge
Are you sure you have it installed correctly? Also have you then restarted your server?Killebrew
You're right Rizier123. Sorry, my mistake, one line was commented. All works, thanks!Litharge
A
6

You get the colored output when you ínstall and enable Xdebug:

Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.

You can enable/disable this with the ini setting xdebug.overload_var_dump

By default Xdebug overloads var_dump() with its own improved version for displaying variables when the html_errors php.ini setting is set to 1. In case you do not want that, you can set this setting to 0, but check first if it's not smarter to turn off html_errors.

Check the documentation for further information.

Note that you do not want to have the Xdebug extension installed on a production server as it will significantly slow down code execution.

Adali answered 2/8, 2015 at 12:32 Comment(3)
Also never forget to restart the server if you installed it :)Killebrew
Thanks, it's what i looked for.Litharge
To get colored output in CLI mode one has to set this xdebug setting xdebug.cli_color=1.Competitor
T
11

Use this code. I've been using it for years. I can't even remember where it originally came from.

function var_dump_pretty($data, $label='', $return = false) {
    $debug = debug_backtrace();
    $callingFile = $debug[0]['file'];
    $callingFileLine = $debug[0]['line'];

    ob_start();
    var_dump($data);
    $c = ob_get_contents();
    ob_end_clean();

    $c = preg_replace("/\r\n|\r/", "\n", $c);
    $c = str_replace("]=>\n", '] = ', $c);
    $c = preg_replace('/= {2,}/', '= ', $c);
    $c = preg_replace("/\[\"(.*?)\"\] = /i", "[$1] = ", $c);
    $c = preg_replace('/  /', "    ", $c);
    $c = preg_replace("/\"\"(.*?)\"/i", "\"$1\"", $c);
    $c = preg_replace("/(int|float)\(([0-9\.]+)\)/i", "$1() <span class=\"number\">$2</span>", $c);

    // Syntax Highlighting of Strings. This seems cryptic, but it will also allow non-terminated strings to get parsed.
    $c = preg_replace("/(\[[\w ]+\] = string\([0-9]+\) )\"(.*?)/sim", "$1<span class=\"string\">\"", $c);
    $c = preg_replace("/(\"\n{1,})( {0,}\})/sim", "$1</span>$2", $c);
    $c = preg_replace("/(\"\n{1,})( {0,}\[)/sim", "$1</span>$2", $c);
    $c = preg_replace("/(string\([0-9]+\) )\"(.*?)\"\n/sim", "$1<span class=\"string\">\"$2\"</span>\n", $c);

    $regex = array(
        // Numberrs
        'numbers' => array('/(^|] = )(array|float|int|string|resource|object\(.*\)|\&amp;object\(.*\))\(([0-9\.]+)\)/i', '$1$2(<span class="number">$3</span>)'),
        // Keywords
        'null' => array('/(^|] = )(null)/i', '$1<span class="keyword">$2</span>'),
        'bool' => array('/(bool)\((true|false)\)/i', '$1(<span class="keyword">$2</span>)'),
        // Types
        'types' => array('/(of type )\((.*)\)/i', '$1(<span class="type">$2</span>)'),
        // Objects
        'object' => array('/(object|\&amp;object)\(([\w]+)\)/i', '$1(<span class="object">$2</span>)'),
        // Function
        'function' => array('/(^|] = )(array|string|int|float|bool|resource|object|\&amp;object)\(/i', '$1<span class="function">$2</span>('),
    );

    foreach ($regex as $x) {
        $c = preg_replace($x[0], $x[1], $c);
    }

    $style = '
    /* outside div - it will float and match the screen */
    .dumpr {
        margin: 2px;
        padding: 2px;
        background-color: #fbfbfb;
        float: left;
        clear: both;
    }
    /* font size and family */
    .dumpr pre {
        color: #000000;
        font-size: 9pt;
        font-family: "Courier New",Courier,Monaco,monospace;
        margin: 0px;
        padding-top: 5px;
        padding-bottom: 7px;
        padding-left: 9px;
        padding-right: 9px;
    }
    /* inside div */
    .dumpr div {
        background-color: #fcfcfc;
        border: 1px solid #d9d9d9;
        float: left;
        clear: both;
    }
    /* syntax highlighting */
    .dumpr span.string {color: #c40000;}
    .dumpr span.number {color: #ff0000;}
    .dumpr span.keyword {color: #007200;}
    .dumpr span.function {color: #0000c4;}
    .dumpr span.object {color: #ac00ac;}
    .dumpr span.type {color: #0072c4;}
    ';

    $style = preg_replace("/ {2,}/", "", $style);
    $style = preg_replace("/\t|\r\n|\r|\n/", "", $style);
    $style = preg_replace("/\/\*.*?\*\//i", '', $style);
    $style = str_replace('}', '} ', $style);
    $style = str_replace(' {', '{', $style);
    $style = trim($style);

    $c = trim($c);
    $c = preg_replace("/\n<\/span>/", "</span>\n", $c);

    if ($label == ''){
        $line1 = '';
    } else {
        $line1 = "<strong>$label</strong> \n";
    }

    $out = "\n<!-- Dumpr Begin -->\n".
        "<style type=\"text/css\">".$style."</style>\n".
        "<div class=\"dumpr\">
        <div><pre>$line1 $callingFile : $callingFileLine \n$c\n</pre></div></div><div style=\"clear:both;\">&nbsp;</div>".
        "\n<!-- Dumpr End -->\n";
    if($return) {
        return $out;
    } else {
        echo $out;
    }
}
Tryout answered 2/8, 2015 at 12:33 Comment(0)
A
6

You get the colored output when you ínstall and enable Xdebug:

Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.

You can enable/disable this with the ini setting xdebug.overload_var_dump

By default Xdebug overloads var_dump() with its own improved version for displaying variables when the html_errors php.ini setting is set to 1. In case you do not want that, you can set this setting to 0, but check first if it's not smarter to turn off html_errors.

Check the documentation for further information.

Note that you do not want to have the Xdebug extension installed on a production server as it will significantly slow down code execution.

Adali answered 2/8, 2015 at 12:32 Comment(3)
Also never forget to restart the server if you installed it :)Killebrew
Thanks, it's what i looked for.Litharge
To get colored output in CLI mode one has to set this xdebug setting xdebug.cli_color=1.Competitor
L
5

In xdebug 3 there is no more xdebug.overload_var_dump setting. When you upgrade to xdebug 3 and you use xdebug.mode=debug as said in xdebug upgrade guide, you will not get colored output anymore. To get the same result as it was in xdebug 2 you must set xdebug.mode to develop,debug

xdebug.mode=develop,debug

Of course if you only want colored output you can only use develop mode but then you lost step debugging. With both options you get colored output and also step debugging. You can specify as many modes as you want/need. They must be separated with ,. All available modes are explained here https://xdebug.org/docs/all_settings#mode

Lenka answered 4/3, 2021 at 5:13 Comment(0)
U
2

You can also use this extension for a colored debug: (this is very easy to install)

http://www.sitepoint.com/var_dump-introducing-symfony-vardumper/

Symfony VarDumper is a component designed to replace your var_dumps. It performs essentially the same functionality, but provides you with much, much more information in a much prettier format. It’s the var_dump you’ve always wanted.

enter image description here

Unscramble answered 2/8, 2015 at 12:34 Comment(0)
E
1

Xdebug is what you are looking for. Sample scripts for install on Ubuntu:

[Search for Xdebug]

$ apt-cache search xdebug

[Install Xdebug]

$ sudo apt-get install php-xdebug

[Restart Apache to make it work]

$ sudo /etc/init.d/apache2 restart
Eley answered 4/8, 2016 at 6:26 Comment(0)
R
0

In latest version of xdebug (now is 3) you should just :
1 . open php.ini
2 . change (xdebug.mode=debug) to (xdebug.mode=develop,debug)

Rearmost answered 16/10, 2022 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.