PHP Output Control

Beginner
10 min read
Updated: Nov 2026
3 examples
ob_* buffering

Definition and Usage

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.

Turn a buffer on with ob_start(). Copy the text with ob_get_contents() or take it and end the buffer with ob_get_clean(). Send the buffer with ob_flush() or ob_end_flush(); clear it with ob_clean() or ob_end_clean().

Inspect helpers such as ob_get_length(), ob_get_level(), and ob_get_status() show size, nesting, and status. flush() pushes system write buffers—not the same job as ob_flush(). Handlers like ob_gzhandler() and rewrite helpers output_add_rewrite_var() / output_reset_rewrite_vars() sit in the same family.

This page explains the ideas. The Output Control Functions index lists all 17 function guides.

Why buffering exists.

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.

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();

Core Ideas

What to remember when you work with PHP Output Control:

IdeaFunctionRemember
Start bufferob_start()Turns buffering on; optional callback can filter every flush of that level
Copy bufferob_get_contents()Returns the text without ending the buffer; pair with clean/flush when done
Capture & endob_get_clean()Most common beginner pattern: get the string and turn the buffer off in one call
Send bufferob_flush()Sends the active buffer and keeps it open; ob_end_flush() sends and ends
Clear bufferob_clean()Erases contents but keeps buffering; ob_end_clean() clears and ends
Size & levelob_get_length() / ob_get_level()Length is bytes in the top buffer; level is how many nested buffers are active
Statusob_get_status()Array of flags and names for the active buffer (or all levels)
System flushflush()Pushes OS/SAPI write buffers; does not empty an ob_start() buffer
Handlersob_list_handlers()Lists active handler names; ob_gzhandler() is a gzip callback for ob_start()
Rewrite varsoutput_add_rewrite_var()Adds name/value pairs for URL/form rewriting; reset with output_reset_rewrite_vars()

Nested levels and handlers

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.

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:

GroupTypical jobStart here
Start & inspectTurn buffering on; read length, level, status, and handlersob_start(), ob_get_level()
Send & clearFlush, discard, or capture buffer contentsob_get_clean(), ob_flush()
Handlers & flush helpersSystem flush, gzip callback, implicit flush modeflush(), ob_gzhandler()
URL rewrite varsAdd or clear name/value pairs used in output rewritingoutput_add_rewrite_var()

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.

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().

PHP
<?php
ob_start();
echo 'line one', PHP_EOL;
echo 'line two', PHP_EOL;

$peek = ob_get_contents();   // copy, buffer stays
$text = ob_get_clean();      // take string and end buffer

echo 'peek length: ', strlen($peek), PHP_EOL;
echo 'captured:', PHP_EOL;
echo $text;
?>

How It Works

  1. ob_start() opens a buffer so the two echo lines are stored, not sent yet.
  2. ob_get_contents() copies the buffer text while buffering stays on.
  3. ob_get_clean() returns the same text and turns the buffer off.
  4. After that, normal echo prints the length and the captured string to the real output.
  5. This is the beginner pattern for turning printed HTML into a variable you can edit or store.

Example 2 — flush() versus ob_flush()

Show that flush() does not empty an output buffer, while ob_flush() sends it.

PHP
<?php
ob_start();
echo 'still inside buffer';
flush();                          // does NOT send the buffer
$afterFlush = ob_get_length();

ob_flush();                       // sends buffer, keeps buffering
$afterObFlush = ob_get_length();
ob_end_clean();

echo 'length after flush(): ', $afterFlush, PHP_EOL;
echo 'length after ob_flush(): ', $afterObFlush, PHP_EOL;
?>

How It Works

  1. ob_start() holds still inside buffer (19 characters including spaces).
  2. flush() runs, but the length stays 19—the text never left the buffer.
  3. ob_flush() sends that text to the next layer and empties the active buffer, so length becomes 0.
  4. The sent text appears first in the sample output; then we print the two measured lengths.
  5. Remember: need both jobs? Call ob_flush() then flush() on setups that buffer at the system layer.

Example 3 — Nested buffer levels

Start two buffers, end the inner one, and watch ob_get_level() drop from 2 to 1.

PHP
<?php
ob_start();
echo 'outer-';
echo 'level A: ', ob_get_level(), PHP_EOL;

ob_start();
echo 'inner';
echo 'level B: ', ob_get_level(), PHP_EOL;
$inner = ob_get_clean();          // ends inner; level back to 1

echo 'got inner: ', $inner, PHP_EOL;
echo 'level C: ', ob_get_level(), PHP_EOL;

$outer = ob_get_clean();
echo $outer;
?>

How It Works

  1. The first ob_start() makes level 1. Text written there waits in the outer buffer.
  2. The second ob_start() makes level 2. New echoes go to the inner buffer only.
  3. ob_get_clean() on the inner buffer returns inner (plus the level B line) and drops back to level 1.
  4. That returned string is echoed into the still-active outer buffer, then the outer buffer is cleaned at the end.
  5. Nested levels let a helper capture its own prints without destroying the page buffer around it.

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.

Start withWhy
flush()Push system write buffers (not the same as ob_flush)
ob_start()Turn on an output buffer
ob_get_clean()Capture buffered text and end the buffer
ob_flush()Send the active buffer and keep buffering
ob_get_level()See how many nested buffers are active
ob_end_clean()Discard the top buffer and turn it off
ob_gzhandler()Gzip callback you can pass to ob_start()

Full list: PHP Output Control Functions index (/php/output-control/function).

Frequently Asked Questions

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.

Browse every Output Control function

Open the functions index for categorized guides with examples and FAQs.

Output Control Functions Index →

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.

9 people found this page helpful