How to print a debug log?

04/10/2020 10:00:02

I'd like to debug some PHP code, but I guess printing a log to screen or file is fine for me.

How should I print a log in PHP code?

The usual print/printf seems to go to HTML output not the console.

I have Apache server executing the PHP code.

Verified Answer (183 Votes)

05/21/2011 09:17:37

A lesser known trick is that mod_php maps stderr to the Apache log. And, there is a stream for that, so file_put_contents('php://stderr', print_r($foo, TRUE)) will nicely dump the value of $foo into the Apache error log.

183

Answer #2 (138 Votes)

11/10/2012 22:57:37
error_log(print_r($variable, TRUE)); 

might be useful

138

Answer #3 (25 Votes)

05/21/2011 08:57:38

You can use error_log to send to your servers error log file (or an optional other file if you'd like)

25
3
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.