We are going to deploy the new yiiframework.com website on March 23, 2018 from 8:00 to 12:00 UTC.
Website and Yii documentation will not be available in this time frame. Check the news for more details.

Yii 2.0: Debuging variables in Yii2


This tutorial is explained following "basic" application structure.

Create functions.php inside config folder and place this code:

 * Debug function
 * d($var);
function d($var,$caller=null)
        $caller = array_shift(debug_backtrace(1));
    echo '<code>File: '.$caller['file'].' / Line: '.$caller['line'].'</code>';
    echo '<pre>';
    yii\helpers\VarDumper::dump($var, 10, true);
    echo '</pre>';
 * Debug function with die() after
 * dd($var);
function dd($var)
    $caller = array_shift(debug_backtrace(1));

Open config/web.php file and include functions.php :

/* Include debug functions */
$params = require(__DIR__ . '/params.php');
$database = require(__DIR__ . '/database.php');

Now you can use debug functions like this:


These debug functions will display what variables contain in readable manner, as well as display where the debug call has been called (in which file and on what line of file).

Total 3 comments

#19001 report it
EvgeniyRRU at 2015/02/21 02:35am
Use xdebug

And if you use IDE, you may to create live template. In PhpStorm I've created var_dump Live Template.

I just typing vd, then tab. Pretty useful

#18994 report it
johonunu at 2015/02/20 02:24am
Fast var dumps

This is for people who don't use xdebug or want to get var dumps fast.

Here is an issue about it: https://github.com/yiisoft/yii2/issues/7352

#18993 report it
merge at 2015/02/19 09:42pm
Php have debugger

Isn't better use xdebug?

Leave a comment

Please to leave your comment.

Write new article