Echoing: dd() vs var_dump() vs print_r()

One of the most popular way of debugging in PHP still remains the same – showing variables in the browser, with hope to find what the error is. Laravel has a specific short helper function for showing variables – dd() – stands for “Dump and Die”, but it’s not always convenient. What are other options?

First, what is the problem with dd()? Well, let’s say we want to get all rows from DB table and dump them:

$methods = PaymentMethod::all();
dd($methods);

We would see something like this:

0915_laravel_dump01

Oh, ok, three rows. Now what are the values? We have to click to expand it – ok, let’s do that:

0915_laravel_dump02

Mmm, now we know which type we are dealing with – “PaymentMethod”. [sarcasm]really helpful…[/sarcasm]
Let’s expand one of the rows now – maybe we will finally see the values?

0915_laravel_dump03

Nope. The final step is to expand attributes or original sections.

But you get the point – to see the actual values, we need to click three additional times, and we don’t see the full result without those actions. At first I thought – maybe dd() function has some parameters for it? Unfortunately not. So let’s look at other options:

var_dump() and die()

Good old PHP way of showing the data of any type:

$methods = PaymentMethod::all();
var_dump($methods);
die();

What we see now:

0915_laravel_dump04

Not easily readable, is it? The trick here is to View Source in browser, in case of Chrome/Windows it’s CTRL+U:

0915_laravel_dump05

Better, huh? But there’s even more readable way.

Have you tried our tool to generate Laravel adminpanel without a line of code?
Go to QuickAdminPanel.com

print_r()

Another PHP built-in function print_r() has a perfect description for us: “Prints human-readable information about a variable”

So if we do this:

$methods = PaymentMethod::all();
print_r($methods);
die();

And then go to View Source of the browser… We get this:

0915_laravel_dump06

Now we can read the contents easily and try to investigate the error.

Moreover, print_r() function has another optional parameter with true/false values – you can not only echo the variable, but return it as string into another variable. Then you can combine several variables into one and maybe log it somewhere, for example.

So, in cases like this, dd() is not that convenient – PHP native functions to the rescue. But if you want the script to literally “dump one simple variable and die” – then dd($var) is probably the fastest to type.

Like our articles?
Check out our Laravel online courses!

22 COMMENTS

  1. Short note: to make var_dump() to show output nicely without viewing source, just surround it with <pre> html tag like this: echo'<pre>’.__FILE__. ‘:’.__LINE__;var_dump($var) ;echo’ </pre>’;. If your IDE supports live templates it is very way to quickly debug full variable 🙂

    • I use this “echo ‘

      '; var_dump(); echo '

      ‘;” and I am a fan of this, added benefit it works with every project I work on no matter what framework it is using. I may add the file and line to it since that sounds like it would be useful.

    • While this works well with echo, it falls flat when you try to feed the string returned by print_r from a controller to its view through a with() method,. Since the framework encodes the pre tag, it shows up as

       ... 

      in the rendered page. If you look in the source, you’ll see that html entities have replaced the angle brackets. While this works nicely for the elements of an array, it prevents the pre tag from doing its job, so you end up with a glop of keys and values.

      However, if you move the tag into your view, and stick to passing the dump, perhaps accompanied by a label, into the view, you get the desired outcome.

  2. Just made it for dump stacking purposes. 🙂
    stack stack stack and echo or return where you want it to be seen, can even log it if you want.


    function prettyDump(){;
    //exit??
    $exit = false;
    $print = false;
    $return = false;
    foreach(func_get_args() as $arg){
    if($arg=='exit') $exit = true;
    if($arg=='print') $pint = true;
    if($arg=='return') $return = true;
    $GLOBALS['trash'][] = $arg;
    }

    if($exit||$print||return){
    ob_start();
    echo "

    ";
    foreach($GLOBALS['trash'] as $someshit){
    if(ini_get('xdebug.max_nesting_level')>count($someshit, COUNT_RECURSIVE))
    var_dump($someshit);
    else
    print_r($someshit);
    }
    echo "

    ";
    }

    if($exit){
    exit(ob_get_clean());
    }

    if($print){
    echo ob_get_clean();
    }

    if($return){
    return ob_get_clean();
    }
    }

  3. `dd()` means “dump and die”. If you don’t want to die, then just use `dump()`.

    If you want to open all levels at once, then hold down the shift or ctrl key when clicking on the “open” icon. It will open all levels below it in one go.

  4. This is fun. `dump()` was created precisely because var_dump and print_r are not that convenient when you want to look at a big array of data. I guess everything old is new again.

LEAVE A REPLY

Please enter your comment!
Please enter your name here