rexkogitans
rexkogitans

Reputation: 314

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?

Upvotes: 3

Views: 3753

Answers (1)

Barmar
Barmar

Reputation: 782488

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.

Upvotes: 4

Related Questions