Search code examples
php12factor

If a PHP application writes its event stream unbuffered to stdout, where does the actual output go?


I'm speaking coming from a PHP background, but I think this applies to other web-server languages. Isn't stdout where all, as named, standard output goes? Also, isn't that where the HTTP response goes? Am I missing something?


Solution

  • After reading Evert's answer, I think I've cleared up some long held assumptions, so I did an experiment.

    <?php
    echo "Echo output\n";
    fwrite(STDOUT, "stdout output\n");
    ?>
    Post-closing tag output
    

    Running this on a command line shows this:

    Echo output
    stdout output
    Post-closing tag output
    

    But viewing this file through a browser shows this:

    Echo output
    Post-closing tag output
    

    Meaning, depending on the context where PHP is run, its output is directed to different places. If served through a web-server, echo and anything outside of the PHP tags go to the HTTP response (at least on Apache2/PHP5). This HTTP response is different from stdout. If ran via CLI echo goes to stdout.

    This question may also be relevant: PHP stdout on Apache