Avatar ·

How to save var_dump() result to a string in PHP?

Hello everyone! I'm a beginner in PHP and I've encountered this problem.

\n

I need to get a dump of a variable (array or object) as a string to write it to a log file or send it via email for debugging. The var_dump() function shows the data structure perfectly, but it immediately outputs the result to the browser or console. But I need to save this output to a string variable.

\n

Here's an example of the code I'm working with:

\n
<?php\n$data = [\n    'name' => 'Ivan',\n    'age' => 30,\n    'skills' => ['PHP', 'JavaScript', 'Python']\n];\n\n// var_dump outputs directly to the output\nvar_dump($data);\n\n// I need something like:\n// $logString = var_dump_to_string($data);\n// file_put_contents('debug.log', $logString);\n?>
\n

I tried using