Output Buffering commands PHP

The output buffering commands add a layer of buffering controlled by PHP in addition to whatever buffering the Web server uses. Some performance penalty may be incurred by adding another layer of buffering, but you may decide the greater control you have is worth the price.

When ob_start is called, all output by functions such as print and echo are held back in a buffer, a large area of memory. The contents of the buffer may be sent to the browser using ob_end_flush, or it may be thrown away using ob_end_clean. Therefore, these functions allow you to avoid errors created by sending headers after content.

ob_start()
The ob_start function begins output buffering. All text sent by print and similar functions is saved in a buffer. It will not be sent to the browser until ob_end_flush is called. The buffer will also be flushed when the script ends.

<?
//begin output buffering
ob_start();
?>
<HTML>
<HEAD>
<TITLE>ob_start</TITLE>
</HEAD>
<BODY>
<?
print("At this point ");
print(strlen(ob_get_contents()));
print("characters are in the buffer.<BR>\n");
?>
</BODY>
</HTML>
<?
//add a test header
header("X-note: COREPHP");
//dump the contents
ob_end_flush();
?>

ob_end_flush()

The ob_end_flush function halts output buffering and sends the contents of the buffer to the browser.

ob_end_clean()

The ob_end_clean function halts output buffering and eliminates the contents of the buffer. Nothing is sent to the browser.
string ob_get_contents()

The ob_get_contents function returns the contents of the output buffer.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics