PHP Function Reference

PHP ob_start() Function



The PHP ob_start() function turns on the output buffering. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. A callback function can be passed to the function to process the contents of the buffer before it is flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.

The contents of this internal buffer may be accessed using ob_get_contents() function. To output what is stored in the internal buffer, ob_end_flush() can be used. On the other hand, ob_end_clean() function can be used to silently discard the buffer contents.

Output buffers are stackable, which means ob_start() can be called while another ob_start() is active. Just make sure that the ob_end_flush() function is called the appropriate number of times. If multiple output callback functions are active, output will be filtered sequentially in the nesting order.

If output buffering is still active when the script ends, PHP outputs the contents automatically.

Syntax

ob_start(callback, chunk_size, flags)

Parameters

callback

Optional. A callback function used to process the contents of the buffer before it is flushed from the buffer. If callback is not a callable function, this function will return false. If callback returns false original input is sent to the browser. The callback function should have the following parameters:

handler(buffer, phase)
Note: ob_end_clean(), ob_end_flush(), ob_clean(), ob_flush() and ob_start() should not be called from this callback function to avoid undefined behavior. The output buffering functions like print_r($expression, true) or highlight_file($filename, true) should not be called from this callback function.
chunk_size Optional. If provided, the buffer will be flushed after any output call which causes the buffer's length to equal or exceed chunk_size. Default is 0 which means that the output function will only be called when the output buffer is closed.
flags

Optional. It is a bitmask that controls the operations that can be performed on the output buffer. The default is PHP_OUTPUT_HANDLER_STDFLAGS which allows output buffers to be cleaned, flushed and removed. PHP_OUTPUT_HANDLER_STDFLAGS can also be achieved when all flag controls mentioned below are OR'd together.

Each flag controls access to a set of functions, as described below:


Return Value

Returns true on success or false on failure.

Example: using user-defined function with ob_start()

In the example below, a user-defined function called myfunc is created which replaces every element of search array present in the buffer string with respective element of replace array.

<?php
function myfunc($buffer) {
  //replace every element of search array 
  //present in the buffer will be replaced 
  //by the respective element of replace array.
  $search = ['fox', 'dog', 'brown'];
  $replace = ['wolf', 'cat', 'black'];

  return (str_replace($search, $replace, $buffer));
}

ob_start("myfunc");
?>

<html>
<body>
<p>The quick brown fox jumps over the lazy dog.</p>
</body>
</html>

<?php
ob_end_flush();
?>

The output of the above code will be:

<html>
<body>
<p>The quick black wolf jumps over the lazy cat.</p>
</body>
</html>

Example: creating an unerasable output buffer

Consider one more example, which explains how to create an unerasable output buffer.

<?php
ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^ PHP_OUTPUT_HANDLER_REMOVABLE);
?>

❮ PHP Output Control Reference