Understanding ob_start() in PHP: A Guide to Output Buffering

May 16, 2023·

3 min read

Table of contents

No heading

No headings in the article.

ob_start() is a PHP function that turns on output buffering. When output buffering is active, no output is sent from the script (other than headers), instead the output is stored in an internal buffer. This buffer can then be manipulated or modified before being sent to the browser or client.

Output buffering can be useful in a variety of situations, such as:

  • Separating application logic from template files

  • Caching output to improve performance

  • Capturing output for testing or debugging purposes

A common use case for ob_start() is to separate application logic from template files. This can be done by using output buffering to capture the output of a template file and store it in a variable, which can then be manipulated or modified before being sent to the browser or client.

Here's an example of using ob_start() to separate application logic from a template file:

function render_template($template_file, $data) {
  ob_start();
  include $template_file;
  $output = ob_get_clean();

  // Manipulate or modify $output as needed
  $output = str_replace('{name}', $data['name'], $output);
  // or you can use $output = strtr($output, $data);

  echo $output;
}

In this example, the render_template() function takes a template file and some data as input. The function uses ob_start() to capture the output of the template file and store it in the $output variable. The contents of $output are then manipulated or modified as needed (in this case, replacing a placeholder with the value of $data['name']) before being sent to the browser or client.

And here's an example of how to use ob_start() to cache output and improve performance in PHP:

<?php

// Check if the cache file exists and is not too old
$cache_file = "cache.html";
$cache_time = 60; // Cache for 60 seconds
if (file_exists($cache_file) && time() - filemtime($cache_file) < $cache_time) {
  // Serve the cached file
  readfile($cache_file);
} else {
  // Start output buffering
  ob_start();

  // Generate the output
  echo "<html><head><title>My Page</title></head><body><p>Hello, world!</p></body></html>";

  // Get the output and save it to the cache file
  $output = ob_get_clean();
  file_put_contents($cache_file, $output);

  // Serve the output
  echo $output;
}

In this example, we check if a cache file exists and is not too old. If it does, we serve the cached file. If it doesn't, we start output buffering using ob_start(), generate the output, get the output using ob_get_clean(), save it to the cache file using file_put_contents(), and serve the output.

Another example of using ob_start() to capture output for testing purposes:

function test_my_function() {
  ob_start();
  my_function();
  $output = ob_get_clean();

  // Perform tests on $output
}

In this example, my_function() is called and its output is captured using ob_start(). The ob_get_clean() function is then used to retrieve the contents of the output buffer and store them in the $output variable. The contents of $output can then be tested or analyzed as needed.


In conclusion, ob_start() is a powerful PHP function that can be used to capture, manipulate, or modify output before it is sent to the browser or client. It can be used in a variety of situations, such as separating application logic from template files, caching output, or capturing output for testing or debugging purposes.