rexkogitans rexkogitans - 16 days ago 5
Apache Configuration Question

PHP stdout not written

I am writing a PHP script running on an Apache httpd web server (not administrated by me). In a function, the variable

$outfile
is given as function parameter, and the very beginning of the function is this:

if (isset($outfile))
$fh = fopen($outfile, 'wb');
else
$fh = fopen('php://stdout', 'w');
echo 'Hello, ';
fwrite($fh, 'World');


I call this function with
$outfile = NULL
, so
$fh
should point to stdout. I can strip down this even more by dropping the condition:

$fh = fopen('php://stdout', 'w');
echo 'Hello, ';
fwrite($fh, 'world');


The point is, it only outputs

Hello,


instead of what I am expecting:

Hello, world!


How come and how can I circumvent this problem?

Answer

Use php://output instead of php://stdout. As the documentation says

php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo.

php://stdout writes directly to the stdout file descriptor, so it won't be properly synchronized with the buffered output of echo.