22 Votes

PHP: Write output of var_dump() to string

Question by Guest | Last update on 2021-07-01 | Created on 2016-06-08

In order to show the content of a PHP variable, you can simply use the function var_dump(). For example, you can just write "var_dump($arr);" for showing the content of the array $arr in PHP.

Unfortunately, the result of var_dump() is always written directly to the webpage. With this, it is not possible to store the result in some string variable in order to process it further or later.

However, I am searching for a function that can help me to achieve that somehow. So, is there any possibility to execute something like var_dump() and to save the result in a variable (a string)?

ReplyPositiveNegativeDateVotes
13 Votes

With using the function print_r() you can do what you want. You only have to pass "true" as a second parameter to the function and print_r() will no longer print out the results directly. Instead, it will return the result as a string:

$arr = array(1,2,3);
$str = print_r($arr, true);

Please note that the information provided by print_r() is not as detailed as the information coming from var_dump(), but perhaps, this can nevertheless help you.
2016-06-09 at 09:21

ReplyPositive Negative
46 Votes

If you want to have exactly the same output as provided by var_dump(), you can use output buffering:

ob_start();
var_dump($someVar);
$str = ob_get_clean();

With ob_start(), the output buffering is started. After that, the output of your script will no longer be sent to the browser. Instead, it is cached. Each output of each command you will execute after ob_start(), will be stored to the buffer. So, for example, also with the output of var_dump(). Afterwards, you can read out to the stored buffer with ob_get_clean() and you can save it to any variable.

Theoretically, you can write your own function to handle this:

function var_dump_tostring($var) {
  ob_start();
  var_dump($var);
  return ob_get_clean();
}

After declaring this function, you can just call the function with your variable to get the var_dump result as string.
2016-06-09 at 14:24

ReplyPositive Negative
35 Votes

Alternatively, I also want to suggest the function var_export. This function is working similar to print_r and it returns valid PHP code describing the passed variable.

$arr = array(1,2,3);
$str = var_export($arr, true);

Similar to print_r, you can also pass "true" as a second parameter to get the result returned as string.
2016-06-10 at 10:44

ReplyPositive Negative
-26 Votes

I think, the classic way should not remain unmentioned. You can also output the contents of the array by hand:

foreach($arr AS $key => $value) {
  echo "$key => $value 
"; }

Admittedly, up to now, all of the other suggested solutions are much simpler than that.
2016-06-10 at 21:54

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.