ob_get_flush

Sends the active output buffer, returns its contents as a string, and turns the buffer off.

Signature
ob_get_flush(): string|false
  • Added in PHP 4.3.0

Return value

string|falseReturns the contents of the active output buffer on success, or false on failure.

Errors

Fails and emits an E_NOTICE when there is no active output buffer, or when the active buffer was started without the PHP_OUTPUT_HANDLER_REMOVABLE flag.

ob_get_flush() does two things at once: it sends the buffered output onward (calling the handler with the PHP_OUTPUT_HANDLER_FINAL flag) and returns a copy of the buffer contents to you, then closes the buffer. It is ob_end_flush() and ob_get_clean() combined.

When to reach for it

Reach for it when the response must still go out but you also need a copy: writing the rendered page into a full-page cache file while serving it, logging a generated response body for auditing, or hashing the output to produce an ETag-style fingerprint after the fact.

What trips people up

The returned string is the buffer contents, not necessarily the bytes that leave the process. If the buffer has a transforming handler — gzip, a minifier, a URL rewriter — the handler's return value is what gets sent, while you receive the pre-handler text. Cache the returned string and you may be caching a different document than the client received.

It also closes the buffer, so it is a one-shot operation. To take a copy without ending the buffer, use ob_get_contents() and then ob_flush(). And as with the other getters, a false return means no removable buffer was active, not an empty document.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Write a rendered page into a full-page cache file while still serving it to the visitor.
  • Log a generated response body for auditing without suppressing the response.
  • Compute a checksum of the output after sending it, for a downstream verification log.

Frequently asked questions

What is the difference between ob_get_flush and ob_get_clean?

Both return the buffer contents and close the buffer; ob_get_flush() also sends the contents onward, while ob_get_clean() discards them so nothing is printed.

Does ob_get_flush keep the buffer open?

No, it closes the active buffer and drops the nesting level by one. Use ob_get_contents() followed by ob_flush() if you need the buffer to stay open.

Does the returned string include changes made by the output handler?

No. You get the raw buffer contents, while the handler's transformed return value is what is actually sent — so a gzip or minifying handler makes the two differ.

More examples

Caching a page while serving it

The paragraph is printed first, then the cache line reporting 23 bytes. One pass produced both the live response and the stored copy.

Example · php
<?php
ob_start();
echo "<p>Order confirmed</p>\n";

$body = ob_get_flush();          // visitor sees it now
file_put_contents('page.cache', $body);

echo "cached ", strlen(file_get_contents('page.cache')), " bytes\n";
unlink('page.cache');

ob_get_flush versus ob_get_clean

Only '[A]' appears before the newline, yet both variables hold their text. The pair shows the single difference between the two functions: whether the buffer is sent or discarded on the way out.

Example · php
<?php
ob_start();
echo "[A]";
$a = ob_get_flush();    // [A] is printed

ob_start();
echo "[B]";
$b = ob_get_clean();    // [B] is not printed

echo "\nreturned: $a and $b\n";

Discussion

  • Be the first to comment on this lesson.