PHP Output Buffering
Some of the coolest functions available in PHP are the output buffering functions. If you are reading this, you probably have some idea of how web php pages are delivered from the server to the client, but if you don’t, here is a quick recap:
- User requests any web page
- The web server receives and interprets the request
- The requested file is sent to the PHP processor
- PHP compiles and executes the combination of PHP and HTML sent to it
- The compiled information is sent back to the web server (the page may not be complete yet)
- Your browser downloads the page from the web server
- Start the Output Buffer capturing text
- Send text such as html or echoed strings to the browser (this will be captured by the output buffer)
- Optionally store the buffer in a variable or clear its contents
- Send the buffer to the browser
<?php
ob_start('ob_gzhandler');
echo 'Hello World';
This will gzip (compress) and send “Hello World” to the browser. Notice I left off the closing php tag because it is not required if I do not have any HTML below it and it helps eliminate the possibility of injecting accidental whitespace into your scripts.
<?php
function strong_em( $buffer )
{
$buffer = str_replace('<strongem>', '<strong><em>', $buffer);
$buffer = str_replace('</strongem>', '</em></strong>', $buffer);
return $buffer;
}
ob_start('strong_em');
echo '<strongem>This text will be both bolded and italicized</strongem>
This is one of the more useful implementations of output buffering. It allows you to create custom html tags and convert them into actual html tags. (Don’t forget to make sure your tags are nested properly however) The actual output of that would be:
<strong><em>This text will be both bolded and italicized</em></strong>
This will erase anything currently stored in the output buffer. It does not turn it off, however, so the buffer will continue to capture text.
This will not only erase the content stored in the output buffer, it will also close that output buffer. If you have several output buffers open, it will only close the top most buffer (the oldest opened buffer).
Ob_get_flush will return the contents of the output buffer as a string. It will then flush the buffer and end output buffering. As a reminder, flushing the buffer sends it the the browser (it’s like echo).
string ob_get_contents ( void )
Ob_get_contents will return the contents in the buffer as a string. It does not erase the buffer or close it. If you echo the variable that you stored the buffer in, and allow the buffer to flush at the end of the script, it will be displayed twice.
This will return the contents of the output buffer and erase its contents. It will also end output buffering. It is the same as calling ob_get_contents() and ob_end_clean(); This is the output buffering function I find that I use most.
ex:
<?php
function echoFive() { echo 'five'; }
ob_start(); echoFive(); $five = ob_get_clean(); // contains the string 'five'
This is a simple example, but if you are working with a function that echos information that you need to filter or store in a variable, it is an excellent way to capture that content into a variable.
This function will close the outer most output buffer and flush its contents to the browser (again, like echo).
This function will simply flush the contents of the output buffer, the output buffer will continue to capture output after being flushed however.
Returns the current number of output buffers that are open.
Returns the number of characters in the output buffer.
array ob_get_status ([ bool $full_status =FALSE ])
Returns that status of the current output buffer. If $full_status is set to TRUE, this function will return the status for all the output buffers, not just the current one.
Array
(
[level] => 2
[type] => 0
[status] => 0
[name] => default output handler
[del] => 1
)
This is an example of return value from ob_get_status.
Returns the list of callback functions passed to all the open calls to ob_start. For example, if you have ob_start() and ob_start(’gz_handler’), you will get an array with the values of 0=>default output handler, 1=>ob_gzhandler
It is important to close all of the output buffers you open because you many not realize which buffer you are getting the information from otherwise. However, if you call ob_start() at the begining of your PHP script, you can avoid potential white space injection issues. If you are trying to set a header value for the return page ( or a cookie, which is set in the response header) you cannot have text sent to the browser before those calls. If you call ob_start() before you make any of those calls, at the end of the script execution the buffer will automatically close and send the content to the browser, preventing anything from being sent to the browser until the very end of your script.
Another great use of output buffering is to gzip the content you are sending back the browser. Just call ob_start(’ob_gzhandler’) and when the script finishes all of the content to be sent to the browser will be gzipped before being sent. The nice thing about ob_gzhandler is that it will auto determine the type of compression (if any) the browser requesting the page can handle and return it’s output accordingly. Just a note however – for gzipping to work, your PHP installation must have the ZLib extension intalled.
Well, that’s all I can think of for output buffering at the moment. However, if you have any questions, please send them through comments and I will not only do my best to answer them, but also make sure that I can include the information in this article. Feel free to digg this page if it has helped. Happy PHPing!
Stumble Upon
Del.icio.us
Buzz

What’s new for October » Blueberryware
October, 22nd 2008 at 8:22 pm
[...] Email 2.0 will be using add a “lazy mode.” Lazy mode will use output buffering to scrape the entire page for email addresses and escape them, you won’t even need to use the [...]