SHARE
Facebook X Pinterest WhatsApp

Output Buffering Page 3

Jul 20, 2010
ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More



With PHP3, all of the content produced
by echo () or print() was sent directly to the server. In PHP4, an
intermediate storage layer makes it possible to defer the sending the Web
server’s output flow. To do this, features so new they have yet to be documented, are available:

  • ob_start(): activates
    output buffering

  • ob_end_flush(): sends the
    contents of the output buffer and deactivates buffering

  • ob_end_clean(): empties the
    output buffer and deactivates buffering

  • ob_get_contents(): returns
    the output buffer contents

This functionality makes it possible to prepare the
contents displayed via complex processes requiring several
database queries, for example, and the sending of results only when all of
the queries have been carried out successfully. In the opposite case, users
can empty the output buffer contents and display an error message in its
place. The example below shows a typical case of how these functionalities
are used:

<?php
// activate output
buffers
ob_start();
// the output is not displayed, it
enters the buffer print “Hello, world!”;
// read buffer
contents
$output = ob_get_contents();
// deactivate the
buffer, empty the buffer
ob_end_clean();
// display
contents
print
$output;
?>

Recommended for you...

What Is a Container? Understanding Containerization
What Is a Print Server? | How It Works and What It Does
Nisar Ahmad
Dec 8, 2023
What Is a Network Policy Server (NPS)? | Essential Guide
Virtual Servers vs. Physical Servers: Comparison and Use Cases
Ray Fernandez
Nov 14, 2023
ServerWatch Logo

ServerWatch is a top resource on servers. Explore the latest news, reviews and guides for server administrators now.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.