PHP ob_get_contents() Function: Everything You Need to Know
As a PHP developer, you may need to get the contents of the output buffer. The ob_get_contents() function is a built-in function in PHP that allows you to get
Normally, when your script echos or prints something, PHP sends it straight to the browser. Output buffering lets you intercept that output and hold it in memory instead, so you can capture, modify, or discard it before it reaches the client. The ob_get_contents() function is the tool that lets you read what is currently sitting in that buffer.
This chapter explains what ob_get_contents() returns, when to reach for it, the gotchas to watch out for, and how it fits alongside the other output-control functions.
What ob_get_contents() does
ob_get_contents() returns the contents of the active output buffer without clearing it or stopping buffering. The buffer keeps collecting output, so you can call the function multiple times to peek at what has accumulated so far.
It only works while a buffer started by ob_start() is active. If there is no active buffer, it returns false.
Syntax
ob_get_contents(): string|false- Parameters: none.
- Return value: the current buffer contents as a string, or
falseif output buffering is not active.
A basic example
<?php
ob_start(); // 1. Start capturing output
echo "This will be buffered"; // 2. Goes into the buffer, not the screen
$output = ob_get_contents(); // 3. Read the buffer into a variable
ob_end_clean(); // 4. Discard the buffer and stop buffering
echo "Captured: " . $output;This prints:
Captured: This will be bufferedHere ob_start() begins buffering, so the echo is held in memory rather than sent to the browser. ob_get_contents() copies that text into $output. Finally ob_end_clean() throws the buffer away and turns buffering off — without it, the buffered text would still be flushed to the page.
ob_get_contents() does not empty the buffer
A common point of confusion: reading the buffer leaves it untouched. The output is still there and will be sent to the browser when buffering ends (unless you clean it).
<?php
ob_start();
echo "Hello";
$first = ob_get_contents(); // "Hello"
echo " World";
$second = ob_get_contents(); // "Hello World" — the buffer kept growing
ob_end_flush(); // sends "Hello World" to the browser
var_dump($first, $second);This outputs:
Hello World
string(5) "Hello"
string(11) "Hello World"If you instead want to read and erase the buffer in one step, use ob_get_clean().
When would I use this?
- Capturing rendered output. Render a template or include a file, then grab the result as a string instead of printing it — handy for emails, caching, or generating a file.
- Post-processing HTML. Buffer a page, read it with
ob_get_contents(), run a transformation (minify, replace placeholders), then echo the result. - Suppressing or inspecting output from a function or library that prints directly.
<?php
function renderGreeting(string $name): void
{
echo "<p>Hello, {$name}!</p>";
}
ob_start();
renderGreeting("Ada");
$html = ob_get_contents(); // capture instead of printing
ob_end_clean();
$html = str_replace("Hello", "Welcome", $html);
echo $html;This outputs:
<p>Welcome, Ada!</p>Gotchas
- Returns
false, not an empty string, when no buffer is active. Use a strict check (=== false) if you need to tell "no buffer" apart from "empty buffer". - Nested buffers:
ob_get_contents()reads only the innermost (current) buffer level. To see how many buffers are stacked, useob_get_level(). - It does not stop buffering. Pair it with
ob_end_clean()orob_end_flush()when you are done.
Related functions
ob_start()— turn output buffering on.ob_get_clean()— get the buffer contents and discard them.ob_get_length()— get the buffer length in bytes.- PHP output control overview — the full family of
ob_*functions.
Conclusion
ob_get_contents() reads the current output buffer as a string without clearing it or ending buffering. It is the building block for capturing rendered output, post-processing HTML, and suppressing direct output. Remember that it returns false when no buffer is active, and that it leaves the buffer intact — reach for ob_get_clean() when you want to read and clear in one move.