PHP: How to write a message to the console (stdout / stderr)
PHP can run in a number of different contexts — as a command line application (CLI) or as an Apache module, for example. Depending on the context, the logging functions behave differently.
In this blog post we look at the scenario of a PHP application providing a REST interface. The PHP code is executed by PHP-FPM.
Messages emitted with print or echo end up directly in the body of the HTTP response in this case. To make messages land on the console in this scenario, you have to use the following construct:
// write to stderr
file_put_contents( "php://stderr","---> file_put_contents stderr" );
// write to stdout
file_put_contents( "php://stdout","---> file_put_contents stdout" );
stdout is the standard output stream of a process, and stderr is its
standard error stream. Both channels are normally connected to the console. You
can find more detail on these standard streams
here.
About Netcup (advertisement)
The German host Netcup offers, among other things, affordable and powerful web hosting packages, KVM-based root servers and dedicated servers. With our voucher codes you can save even more (6€ off your first order, 30% off all KVM-based root servers, ...).