PHP flush() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Output Control

Definition

In PHP, flush() asks PHP to push any data sitting in the system write buffers out to the operating system (and toward the web server or CLI).

It does not empty or send PHP's output buffering stack. If you called ob_start(), echo still sits in that buffer until you use ob_flush(), ob_end_flush(), or a related function. Think of flush() as "send what has already left the output buffers," and of ob_flush() as "send the current output buffer."

Syntax

Call flush() with the values you need. Start with this line.

PHP
flush();
CallWhat it does
flush()Pushes system write buffers toward the OS or SAPI.
echo "hi"; flush();Writes text, then asks PHP to flush system buffers.
ob_start(); echo "x"; flush();flush() does not send the buffered "x"; it stays in the output buffer.

On CLI, flush() often has little visible effect because output already goes out quickly. Behind a web server, proxies, and gzip handlers may still delay what the browser sees. flush() is not a substitute for ob_flush().

Library declarations
PHP
flush(): void

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
voidNo return value.

PHP Examples

Example 1 — Calling flush After Echo

Echo two lines and call flush() between them. The printed text is the same with or without flush(); the call only nudges system buffers.

PHP
<?php
echo "line one", PHP_EOL;
flush();
echo "line two", PHP_EOL;

How It Works

  1. The first echo writes "line one" and a newline.
  2. flush() asks PHP to push system write buffers outward.
  3. The second echo writes "line two".
  4. In CLI you see both lines either way; flush() does not change the text, only when buffers are written.

Example 2 — flush Does Not Empty an Output Buffer

Start buffering, echo text, call flush(), then read the buffer. The text is still there because flush() is not ob_flush().

PHP
<?php
ob_start();
echo "still buffered";
flush();
$copy = ob_get_contents();
ob_end_clean();
echo "captured: ", $copy, PHP_EOL;

How It Works

  1. ob_start() turns on an output buffer so echo is stored, not sent yet.
  2. echo writes "still buffered" into that buffer.
  3. flush() runs, but the string stays in the buffer because flush() does not send output-buffer contents.
  4. ob_get_contents() copies the text, ob_end_clean() discards and turns the buffer off, then we print what was captured.

Example 3 — Length Unchanged After flush

Measure the buffer length before and after flush(). The length stays the same, proving flush() did not clear or send the buffer.

PHP
<?php
ob_start();
echo "hello";
$before = ob_get_length();
flush();
$after = ob_get_length();
ob_end_clean();
echo "before=", $before, " after=", $after, PHP_EOL;

How It Works

  1. Buffering starts and echo stores the five characters of "hello".
  2. ob_get_length() reports 5 before flush().
  3. flush() runs; the buffer length is still 5.
  4. ob_end_clean() clears and ends the buffer, then both lengths are printed.

Frequently Asked Questions

flush() tells PHP to push data from its system write buffers toward the operating system or SAPI. It does not turn output buffering on or off.
flush() flushes system write buffers. ob_flush() sends the contents of the active output buffer started with ob_start() and keeps that buffer active. They solve different problems.
Because echo went into the output buffer. flush() cannot send that buffered text. Call ob_flush() or ob_end_flush() (and often flush() afterward on some setups) when you want buffered output to leave.
No. Its signature is flush(): void. There is nothing to assign or check.
Sometimes, when you want progress text to appear sooner. Many CLI runs already show output quickly, so the effect can be hard to notice.

Next: ob_clean()

ob_clean() erases the contents of the active output buffer but keeps buffering turned on.

ob_clean() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful