Programming

PHP: write var_dump output directly into a log file

I just encountered a bug in a production environment that I could not reproduce in development. In order to debug it without disrupting the users, I used the following snippet to directly send the debug output into a log file.

ob_start();
var_dump($someVariable);
$contents = ob_get_contents();
ob_end_clean();
someLogFunction($contents);

Prefer var_dump to print_r as it would transform NULL and booleans (for example, FALSE would appear as an empty string).

Source

Back


Comments

No comment yet.

A remark, a suggestion? Do not hesitate to express yourself below. Just be courteous and polite, please.

If this field is left blank, you will appear as Anonymous.