PHP ob_start() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Output Control

Definition

In PHP, ob_start() turns on output buffering so echo and other output are stored instead of being sent immediately.

It is the main entry point for the output control family. You can nest buffers; each successful call raises ob_get_level() by one. An optional callback can transform the buffer when it is flushed or ended.

Syntax

Call ob_start() with the values you need. Start with this line.

PHP
ob_start();
CallWhat it does
ob_start()Starts a default output buffer; returns true on success.
ob_start(function ($s) { return strtoupper($s); })Starts a buffer that uppercases content when flushed.
ob_start('ob_gzhandler')Starts a buffer that may compress output for supporting clients.

Output buffering does not delay header() in the same way beginners sometimes hope, but it does let you capture HTML into a string, discard mistakes with ob_end_clean(), or post-process with a callback. Always end buffers you start.

Library declarations
PHP
ob_start(?callable $callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS): bool

Parameters

ParameterDescription
callbackOptional. A callable that receives the buffer string (and flags) when the buffer is flushed or closed. null means the default handler.
chunk_sizeOptional. If greater than 1, the buffer flushes automatically each time it grows by about this many bytes. 0 means no chunk-size auto-flush.
flagsOptional. Bitmask controlling which operations are allowed (clean, flush, remove). The default PHP_OUTPUT_HANDLER_STDFLAGS allows the usual operations.

Return Value

Return ValueDescription
booltrue if the buffer started successfully, false on failure.

PHP Examples

Example 1 — Capture Output Into a String

Start buffering, echo a message, then pull it out with ob_get_clean().

PHP
<?php
ob_start();
echo "Hello Buffer";
$text = ob_get_clean();
echo $text, PHP_EOL;

How It Works

  1. ob_start() begins capturing output.
  2. echo stores text in the buffer instead of sending it yet.
  3. ob_get_clean() returns the string and ends the buffer.
  4. We print the captured text.

Example 2 — Transform With a Callback

Pass a callback that uppercases the buffer when it is flushed and ended.

PHP
<?php
ob_start(function ($chunk) {
    return strtoupper($chunk);
});
echo "quiet please";
ob_end_flush();
echo PHP_EOL;

How It Works

  1. ob_start() registers a callback.
  2. echo stores lowercase text in the buffer.
  3. ob_end_flush() sends the buffer through the callback.
  4. The callback returns the uppercase string that is printed.

Example 3 — Nest Two Buffers

Start two levels, write to each, and show how ending the inner buffer works.

PHP
<?php
ob_start();
echo "outer-";
ob_start();
echo "inner";
$inner = ob_get_clean();
$outer = ob_get_clean();
echo $outer, $inner, " level=", ob_get_level(), PHP_EOL;

How It Works

  1. The outer buffer receives "outer-".
  2. The inner buffer receives "inner".
  3. First ob_get_clean() returns "inner" and lowers the level by one.
  4. Second ob_get_clean() returns "outer-"; nesting is fully closed.

Frequently Asked Questions

It turns on an output buffer so printed output is stored until you flush, clean, or end that buffer.
Yes. Each call nests another buffer and raises ob_get_level() by one until you end those buffers.
PHP passes the buffer contents to your callable when the buffer is flushed or closed, so you can transform or filter the output.
Use ob_get_contents() to copy without ending, ob_get_clean() to get and discard/end, or ob_get_flush() to get and send/end.
No. flush() does not empty the user output buffer. Use ob_flush() or ob_end_flush() (and optionally flush() afterward) when you need buffered text to leave.

Next: output_add_rewrite_var()

output_add_rewrite_var() stores a name/value pair for the URL rewriter and starts the URL-.

output_add_rewrite_var() tutorial →

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.

8 people found this page helpful