ServersOutput Buffering Page 3

Output Buffering Page 3

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;
?>

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories