«

»

Jan 02

How to store a var_dump

one thing i really like about PHP is var_dump, a nice little function that outputs what an object is regardless of type, including classes which shows what data is private and protected. when i need to debug a language one of the first things i look for is a equivalent.

but sometimes the var_dump either gets lost in the page output, breaks it or the var_dump causes you to loose the data you wanted to see. in those cases you might want to output the var_dump to a file…problem is that var_dump will output to the screen and the function itself has no return value

in that case we just use the output buffer. using ob_start we stop stuff being outputted to the screen and instead having it outputted to the output buffer and then using ob_get_clean we clear the output buffer and store it in a variable.

since var_dump outputs to the screen we are then able to get the output as a string from ob_get_clean. bellow is a function i generally use for this.

function storeVarDump($data)
{
    ob_start();
    var_dump($data);
    $varDump = ob_get_clean();
    return $varDump;
}

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>