phpstringvar-dump

How can I capture the result of var_dump to a string?


I'd like to capture the output of var_dump to a string.

The PHP documentation says;

As with anything that outputs its result directly to the browser, the output-control functions can be used to capture the output of this function, and save it in a string (for example).

What would be an example of how that might work?

print_r() isn't a valid possibility, because it's not going to give me the information that I need.


Solution

  • Use output buffering:

    <?php
    ob_start();
    var_dump($someVar);
    $result = ob_get_clean();
    ?>