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
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉
![COil](https://www.strangebuzz.com/media/cache/avatar/img/avatars/coil.webp)