How to Put PHP var_dump() in a Variable

It’s often useful to use var_dump() while debugging but sometimes you aren’t ready to output to the screen yet. Here’s how to store PHP var_dump() in a variable:

function grab_dump($var)
{
	ob_start();
	var_dump($var);
	return ob_get_clean();
}
  • Leave a Comment

  • Ahmedsafan86

    nice post man thanks

  • Dan Brellis

    Very helpful. Thanks for posting this.

  • bill nunney

    very useful in CLI usage, thanks :)

  • Josh Levinson

    var_export($var, true); is a better option.

  • Vincent

    really useful, this feature should however be integrated in the var_dump() function

    var_dump($var, true); would be great

  • jk

    hyahh

  • Gonzalo Jarjury

    Thx so much , saved my life :D

  • http://www.gableroux.com/ Gabriel Le Breton
  • Jamie B

    I found this super useful. I didn’t realize that var_dump printed directly to the screen. We were using it in unit tests and it wasn’t showing up. This method fixes that.
    http://www.redoma.digital