PHP ob_flush() Function

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

Definition

In PHP, ob_flush() sends the contents of the active output buffer and keeps that buffer turned on.

After a flush, the buffer is empty but still active, so new echo calls are captured again. This is different from flush(), which only pushes system write buffers and does not empty an ob_start() buffer.

Syntax

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

PHP
$ok = ob_flush();
CallWhat it does
ob_flush()Sends the active buffer and keeps buffering; returns true on success.
ob_start(); echo "x"; ob_flush();Sends "x"; the buffer stays active and empty.
ob_get_length()Often 0 right after a successful ob_flush().

ob_flush() needs a flushable active buffer. With nested buffers, flushed data goes to the next outer buffer. Pair with flush() only when you also need system buffers pushed; they are separate steps.

Library declarations
PHP
ob_flush(): bool

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Flush While Keeping the Buffer

Flush once, write more, then end-clean so only the second write remains to capture—and the first write was already sent.

PHP
<?php
ob_start();
echo "sent-now";
ob_flush();
echo "kept";
$left = ob_get_clean();
echo PHP_EOL, "left=", $left, PHP_EOL;

How It Works

  1. "sent-now" is buffered.
  2. ob_flush() sends "sent-now" and leaves an empty active buffer.
  3. "kept" is written into that same buffer.
  4. ob_get_clean() returns "kept" and ends buffering; we print it after the already-sent text.

Example 2 — Length After ob_flush

Compare lengths before and after ob_flush() while watching the nesting level stay the same.

PHP
<?php
ob_start();
echo "abcd";
$before = ob_get_length();
$levelBefore = ob_get_level();
ob_flush();
$after = ob_get_length();
$levelAfter = ob_get_level();
ob_end_clean();
echo PHP_EOL, "len ", $before, "->", $after, " level ", $levelBefore, "->", $levelAfter, PHP_EOL;

How It Works

  1. Four characters sit in the buffer.
  2. ob_flush() sends them; length becomes 0.
  3. The nesting level stays 1 because buffering remains on.
  4. ob_end_clean() ends the empty buffer; then we print the measurements.

Example 3 — Not the Same as flush()

Use an outer buffer to catch what ob_flush() sends, and show that flush() alone left the inner text in place.

PHP
<?php
ob_start();
ob_start();
echo "demo";
flush();
$afterSystemFlush = ob_get_contents();
ob_flush();
$afterObFlush = ob_get_contents();
ob_end_clean();
$outer = ob_get_clean();
echo "after flush(): ", var_export($afterSystemFlush, true), PHP_EOL;
echo "after ob_flush(): ", var_export($afterObFlush, true), PHP_EOL;
echo "outer received: ", var_export($outer, true), PHP_EOL;

How It Works

  1. An outer buffer wraps an inner buffer that holds "demo".
  2. flush() runs; the inner copy is still "demo" because flush() does not empty output buffers.
  3. ob_flush() sends "demo" into the outer buffer and leaves the inner buffer empty.
  4. We end both buffers and print the three snapshots.

Frequently Asked Questions

ob_flush() sends the active output buffer contents and keeps buffering on, so the buffer is empty but still active.
ob_flush() sends the ob_start() buffer. flush() only pushes system write buffers and does not empty the output buffer.
ob_flush() keeps the buffer active. ob_end_flush() sends and then turns the top buffer off.
No. It returns true or false. Use ob_get_contents() before flushing, or ob_get_flush() to get the string while flushing and ending.
Check that you really echoed after the flush, and that you did not call an end_* function. After ob_flush(), new echo output should accumulate again at the same level.

Next: ob_get_clean()

ob_get_clean() returns the contents of the active output buffer, then cleans that buffer a.

ob_get_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