«

»

Jul 27

Send var_dump result to a string variable with PHP

Maybe you need to capture var_dump result in a variable to use it like a string in you context. You don’t want to output result but just have it in your hand.

What if var_export doesn’t work

One of the solution can be use var_export function passing a true parameter.

$result = var_export($yourvar, true);

But sometimes this solution doesn’t work as expected so there is a simple and easy workaround. You have to use output buffer. Let’s take a look:

ob_start();
var_dump($yourvar);
$result = ob_get_clean();

A recommendation : use it in a debug environment and not in a production one because a possibly performance issue most of all if you use with large arrays or data.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>