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)?
Related Topics
PHP: Write Output of a Script into Variable
Tutorial | 0 Comments
PHP: Send Output of a Script by Mail
Tutorial | 0 Comments
PHP: Check Strings with Ctype-Functions for Character Classes
Article | 0 Comments
PHP: Current Date and Time
Tutorial | 0 Comments
PHP: How to show Content of Array in a quick way
Tip | 0 Comments
Delphi/Lazarus: Display current Date and Time
Tip | 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.
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:
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
If you want to have exactly the same output as provided by var_dump(), you can use output buffering:
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:
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
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.
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
I think, the classic way should not remain unmentioned. You can also output the contents of the array by hand:
Admittedly, up to now, all of the other suggested solutions are much simpler than that.
2016-06-10 at 21:54