34,333
edits
Changes
→PHP Output Buffering
== PHP Output Buffering ==
Output buffering in PHP is a mechanism whereby content thaty would nornally be sent directly to the outpout stream is initally held in a buffer until the buffer is flushed. This provides control over when output is presented to the user (perhaps in situations where there is a delay in gather some data from a database for example).
Output Buffering is initaited using the ''ob_start()'' function. ''ob_start()'' can be called with no argumnhts but does support three optional arguments:
* callback function - specifies a user defined function to be called before tyhe buffer is flushed
* bytes - an integer representing the number of bytes to be buffered before the callback is triggered. Set to ''null'' to disable this feature
* delete buffer - Boolean value specifying whether buffer should be deleted after ''ob_end()'' call.
The contents of the buffer are flushed using the ''ob_flush()'' function. It is also to possible to both flush and stop future buffering using the ''ob_end_flush()'' function.
Finally the contents of the buffer may inspected at any time using the ''ob_get_contents()'' function which returns a string containing the buffer content.
it is also possible to delete all buffer content without flushing with a call to the ''ob_clean()'' function.
<pre>
<?php
ob_start(); //start buffering
echo "This content will be buffered"; // write some content to the buffer
ob_end_flush(); // flush the output from the buffer
?>
</pre>