Quick debug of a string using the file_put_contents PHP function

Published on 2021-09-05 • Modified on 2021-09-05

In this snippet, we see how to inspect the content of a string using the file_put_contents PHP function. Sometimes, when you use the CLI, your output may be not adapted for the terminal, like HTML. One solution is to redirect the output to a file, but it may be polluted by other content. In this case, it's better to dump what you want into a dedicated file. The file_put_contents function is easy to use as you have to provide the filename and the content to dump. To build the full file name, it's generally convenient to use the __DIR__ constant. In a Symfony project, you can also use the project root directory.

file_put_contents(__DIR__.'/../debug.html', $client->getResponse());
file_put_contents($this->getParameter('kernel.project_dir').'/debug.html', $client->getResponse());

 More on Stackoverflow   Read the doc  Random snippet