PHP Output Control lets you catch what echo and print would send, hold it in a buffer, then capture it as a string, send it later, or throw it away. Without buffering, the first byte of body output locks HTTP headers. With buffering, you can build HTML first and still call header() afterward.
It keeps early echo from locking headers, lets you turn printed text into a string, and supports nested capture so one library can buffer inside another.
flush() is not ob_flush().
ob_flush() sends the active ob_start() buffer. flush() only nudges system write buffers. If text is still held by Output Control, call ob_flush() (or ob_end_flush()) first.
Syntax
PHP Syntax
The usual pattern: start a buffer, write output, then capture, send, or discard it:
PHP
// Start buffering
ob_start(); // bool; level becomes 1
echo 'Hello buffering';
// Inspect
$len = ob_get_length(); // int|false
$level = ob_get_level(); // int
$status = ob_get_status(); // array
$copy = ob_get_contents(); // string|false (buffer stays)
// Capture and end
$text = ob_get_clean(); // string|false; ends buffer
// Or send / clear while buffering
ob_start();
echo 'chunk';
ob_flush(); // send buffer, keep buffering
ob_clean(); // erase contents, keep buffering
ob_end_flush(); // send and end
// ob_end_clean(); // discard and end
// System flush (not the same as ob_flush)
flush();
// Nested levels
ob_start(); // level 1
ob_start(); // level 2
ob_end_clean(); // back to level 1
ob_end_clean();
Reference
Core Ideas
What to remember when you work with PHP Output Control:
Each ob_start() adds a level. Inner buffers must end before outer ones see that text. ob_get_flush() returns the buffer and flushes it in one step. ob_implicit_flush() can make every flush of output also call the system flush path. Prefer explicit ob_flush() + flush() while you learn.
Map
Function Groups at a Glance
The Output Control section is one family with four jobs. Use this map to pick the right group before you open a function page:
Group
Typical job
Start here
Start & inspect
Turn buffering on; read length, level, status, and handlers
Neighboring tutorials that often sit next to this work: MySQLi for database results you may wrap in a buffer, Variables for storing captured strings, Strings for editing buffered HTML, and Filesystem for writing captured output to a file.
Code
PHP Examples
Three short scripts that show why buffering exists, how to capture output, how flush() differs from ob_flush(), and how nested levels work. Each block includes a fixed View Output panel that matches this tutorial layout.
Example 1 — Start buffering and capture
Turn buffering on with ob_start(), print two lines, then take the text with ob_get_clean().
The first ob_start() makes level 1. Text written there waits in the outer buffer.
The second ob_start() makes level 2. New echoes go to the inner buffer only.
ob_get_clean() on the inner buffer returns inner (plus the level B line) and drops back to level 1.
That returned string is echoed into the still-active outer buffer, then the outer buffer is cleaned at the end.
Nested levels let a helper capture its own prints without destroying the page buffer around it.
Continue
Next: PHP Output Control Functions Index
After you know why buffering exists, how to capture with ob_get_clean(), how flush() differs from ob_flush(), and how nested levels work, open the functions index for all 17 guides.
Output Control is PHP's built-in way to catch echo and print in a memory buffer instead of sending them straight to the browser or CLI. You turn buffering on with ob_start(), then capture, send, or discard that text with helpers such as ob_get_contents(), ob_get_clean(), ob_flush(), and ob_end_clean().
It lets you decide headers after you start building HTML, capture a function's printed text into a string, wrap a whole page in a filter (for example gzip), or nest buffers so libraries can capture their own output without breaking yours.
ob_flush() sends the active output buffer started with ob_start() and keeps that buffer open. flush() pushes system write buffers toward the OS or web server. If text is still held by ob_start(), flush() alone will not deliver it—you need ob_flush() (or ob_end_flush()) first.
Call ob_start(), run the code that prints, then use ob_get_clean() to get the string and turn the buffer off. ob_get_contents() copies the buffer without ending it; pair that with ob_end_clean() when you want to discard afterward.
Each successful ob_start() adds one level. ob_get_level() tells you how many are active. Ending the top buffer with ob_end_clean() or ob_end_flush() drops the level by one and reveals the buffer underneath.
Start with ob_start() and ob_get_clean() so you can capture output. Then learn ob_flush() versus flush(), and inspect helpers such as ob_get_length() and ob_get_level(). Open the functions index for all 17 guides.