Holm Log


August 8, 2006

PHP Output Buffering

Filed under: Books — bruce @ 3:48 pm

Chapter 11 in “PHP In A Nutshell” book by Paul Hudson described what output buffering is and how to use it and why.

By default PHP will transfer display information to the client’s web browser as soon as it’s ready to be sent. However the display does not occur until the end of the script has been reached. But this can be slow as it waits for all the bits of data to be computed and restricts you on the order that it’s displayed. With output buffering you can gather chunks or all of the display output and rearrange the chunks if needed or negate displaying them at all based on conditions you choose such as error conditions. It also lets you compress the output before sending it to the client and reduces the amount of data needing to be transferred, however the savings in bandwidth may be negated by the extra CPU time needed to do the compression.

To buffer output, you call the ob_start() function before the output is sent. When the output in this chunk is complete, then you can either call ob_end_flush() which dumps it immediately to the client’s web browser or call ob_end_clean() which clears the buffer. If you wish to hang on to the buffer contents you can either load it into a string var (explained below) before clearing the buffer or you can open a new buffer with a second ob_start() function call instead of the ob_end_clean. This effectively puts the first buffer contents on hold while a new nested buffer collects the output generated from that point on. Calling the ob_end_flush after some output is generated will flush the output in this nested second buffer into the first output buffer. Or again, calling ob_end_clean would clear the second buffer instead, returning further output collects going into the first output buffer again.

Two other functions, ob_flush and ob_clean do the same things as the ob_end_flush and ob_end_clean functions respectively except the output buffer isn’t closed. To read the current output buffer content, use the function ob_get_contents() which returns the buffer as a string. Once the buffer contents are in a string var then you can use it later for display or for writing to a file or based on error or other conditions, decide what to do with it.

Using these functions carefully can result in control over what is output and in what order. One possible use of the output buffering would be to help separate the PHP code used to process a form submission that validates the form input before the display template is generated, allowing the error messages to be collected prior to the page html code is generated.

Another handy function to use is the flush() function which displays the output so far immediately. This is primarily useful when the end of the script has not been reached and you want to display what has been generated so far in the buffer. It can also be useful when the output buffering is not being used but the output content being generated is taking a long time to crunch through.

To compress the output buffer contents, pass the parameter ob_gzhandler to ob_start(). This essentially zips the information before it is sent. The last section of the chapter discusses url rewriting, which allows for adding url parameters on all url references and hidden form elements within the output buffer. This is done using the functions output_add_rewrite_var() function. If you later change your mind and want to undo the added URL and form element additions, then call the function output_reset_rewrite_vars() which removes them.

No Comments »

No comments yet.

RSS feed for comments on this post. | TrackBack URI

Leave a comment

You must be logged in to post a comment.