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?
Use php://output instead of php://stdout. As the documentation says
php://outputis a write-only stream that allows you to write to the output buffer mechanism in the same way asecho.
php://stdout writes directly to the stdout file descriptor, so it won't be properly synchronized with the buffered output of echo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With