Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

echo and flush() in PHP show nothing until the script finishes

ErrorOutput from echo appears all at once when the script ends

Short answer

flush() only empties PHP's own system buffers. It cannot empty a PHP output buffer, and the manual says it may not override the web server's buffering or the browser's. Find out which layer holds the data with curl -N, then call ob_flush() before flush() or turn off nginx buffering for that response.

  • Applies to PHP 8.3, PHP 8.4, PHP-FPM, nginx 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Request the page with curl and its buffering switched off. If the lines arrive one by one, PHP and the server are fine: the manual says flush() has no effect on client-side buffering in the browser, so the browser is the one holding the output. If they arrive all at once, one of the causes below applies.

    curl -N https://example.com/stream.php
  2. 2Match what you see, then jump to its fix

    1. 1curl -N shows nothing until the end, and a debug line shows ob_get_level() above 0A PHP output buffer is active, and flush() does not empty itFix
    2. 2curl -N still shows nothing until the end, although the PHP check above shows no active buffer; the site runs behind nginxnginx buffers the FastCGI or proxied responseFix

What does the error look like?

  • Output from echo appears all at once when the script ends
  • flush() has no visible effect in the browser
  • ob_flush() raises an E_NOTICE when no output buffer is active

Cause 1 of 2

A PHP output buffer is active, and flush() does not empty it

1Check

output_buffering can be switched on for every script, and any code can add its own buffer with ob_start(). The manual says flush() has no effect on user-level output handlers started by ob_start(). Print the nesting level at the top of the script.

<?php
// temporary, at the top of stream.php
header('Content-Type: text/plain');
echo 'output_buffering=', var_export(ini_get('output_buffering'), true), ' level=', ob_get_level(), "\n";

2Fix

Empty the PHP buffer first, then the system buffers: ob_flush() followed by flush(). Call ob_flush() only while a buffer exists, because without one it raises an E_NOTICE. For a script that streams the whole time, set output_buffering to 0 in php.ini instead.

<?php
for ($i = 1; $i <= 5; $i++) {
    echo "step $i\n";
    if (ob_get_level() > 0) {
        ob_flush();
    }
    flush();
    sleep(1);
}

3Verify

Run the same curl command again; one line should appear per second.

curl -N https://example.com/stream.php

Confirmed by php.net

Did this fix it?

Cause 2 of 2

nginx buffers the FastCGI or proxied response

1Check

With buffering on, which is the default, nginx collects the response from PHP-FPM in memory buffers before it passes it on. Look for buffering settings in the site config.

grep -R -n -E "fastcgi_buffering|proxy_buffering" /etc/nginx/

2Fix

Turn buffering off for the location that streams, or let the script switch it off for its own response with the X-Accel-Buffering header, which nginx reads from the upstream response. Use proxy_buffering instead of fastcgi_buffering if nginx proxies to Apache.

# nginx, in the location that serves the script
fastcgi_buffering off;

// or in the PHP script, before the first echo
header('X-Accel-Buffering: no');

3Verify

Reload nginx and run curl -N again.

sudo nginx -t && sudo systemctl reload nginx
curl -N https://example.com/stream.php

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Test streaming with curl -N first, so you know which layer holds the data before you touch any setting; a browser page is a poor test.
  • Check ob_get_level() before calling ob_flush(), so a missing buffer does not raise a notice.
  • Keep streaming endpoints on their own nginx location, so buffering can be off there and on for the rest of the site.
  • Do not turn implicit_flush on for a whole site; the manual says it has serious performance implications on the web and is meant for debugging.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter works locally but every page is 404 on the live server

The requested URL was not found on this server.

A 404 on the live server comes from one of four places: the document root is not the public folder, Apache ignores .htaccess or nginx has no try_files rule, the route is not defined because auto routing is off, or the controller file name differs from its class in case.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.