ob_get_contents

Returns the current contents of the output buffer without emptying or closing it.

Signature
ob_get_contents(): string|false
  • Added in PHP 4.0

Return value

string|falseReturns the contents of the output buffer, or false if output buffering is not active.

ob_get_contents() is the read-only view of the active output buffer. It copies out whatever has been written so far and changes nothing: the buffer keeps its contents, stays open, and the nesting level is untouched. You can call it as many times as you like and watch the string grow.

When to reach for it

Use it when you need to inspect output while still intending to send it — logging a response body while the response is being built, checking whether a template produced anything before deciding to wrap it, or scanning the buffered HTML for a marker before flushing.

What trips people up

The big one is double output. Because the buffer is not emptied, echo ob_get_contents(); while the buffer is still open writes the copy back into the same buffer, so everything appears twice. If you want the string and no output, use ob_get_clean(); if you want both, use ob_get_flush().

It also only ever sees the innermost buffer. With nested ob_start() calls, text written before the inner buffer opened lives in the parent and is invisible until that inner buffer is closed. And like the other getters it returns false when no buffer is active, which an empty buffer's "" can be mistaken for — compare with === false.

Example

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

When to use it

  • Log the response body to a file while the response is still being assembled and will still be sent.
  • Check whether a buffered template produced any output before deciding to render a wrapper around it.
  • Search buffered HTML for a placeholder and decide whether to keep buffering or flush.

Frequently asked questions

Does ob_get_contents clear the buffer?

No. It returns a copy and leaves the buffer open with its contents intact. Use ob_get_clean() to read and discard, or ob_get_flush() to read and send.

Why is my output duplicated when I use ob_get_contents?

Because you echoed the returned string while the buffer was still open, so the copy was written back into the same buffer. Either close the buffer first or use ob_get_clean().

What does ob_get_contents return when nothing has been echoed?

An empty string, as long as a buffer is active. It returns false only when output buffering is not active at all, so test with === false.

More examples

Inspecting output while still sending it

All three lines are printed. The count reads 2 because ob_get_contents() saw only what existed at that moment, and reading it did not stop the earlier lines from being flushed later.

Example · php
<?php
ob_start();
echo "line one\n";
echo "line two\n";

$sofar = ob_get_contents();
echo "lines so far: ", substr_count($sofar, "\n"), "\n";

ob_end_flush();

Only the innermost buffer is visible

The first dump is string(10) "inner text" and the second is string(11) "outer text ". The outer text was never part of the inner buffer, which is why the first call could not see it.

Example · php
<?php
ob_start();                 // outer
echo "outer text ";
ob_start();                 // inner
echo "inner text";

$inner = ob_get_contents();
ob_end_clean();             // drop the inner buffer
$outer = ob_get_contents();
ob_end_clean();

var_dump($inner, $outer);

Discussion

  • Be the first to comment on this lesson.