Output Buffering in PHP

That is a link to my github page!Once you are using output buffering, you can compress content before you send it.

HTML is made up of lots of simple, repeating tags, and normal text on a site is easy to compress, which means that compressing your pages can drastically cut the amount of bandwidth your site uses, as well as how long it takes to transfer a page.

Output buffering generally will not affect the speed of your web server by any amount, unless you choose to compress your content.

Compression takes up extra CPU time; however, the amount of page bandwidth you see will be cut by about 40%, which means your server will spend less time sending data across the network.

Your compression mileage may vary — if you have lots of pictures, this will matter less, if you are sending a lot of XML, your savings will be higher.

There are two ways to start buffering output: through a php.

ini setting to enable output buffering for all scripts, or by using a function call on a script-by-script basis.

The latter is preferred, as it makes your code more portable and also gives you greater flexibility in how you use output buffering.

To create a new output buffer and start writing to it, call ob_start().

There are two ways to end a buffer: ob_end_flush() and ob_end_clean().

The former ends the buffer and sends all data to output, and the latter ends the buffer without sending it to output.

Every piece of text written while an output buffer is open is placed into that buffer, as opposed to being sent to the output.

For example:That script will output “Hello First” because the first text is placed into a buffer and then flushed with ob_end_flush().

The “Hello Second” will not be printed out, though, because it is placed into a buffer that is cleaned using ob_end_clean() and not sent to the output.

Finally, the script will print out “Hello Third” because PHP automatically flushes open output buffers when it reaches the end of the script.

REUSING BUFFERSThe function ob_end_flush() and ob_end_clean() are complemented by ob_flush() and ob_clean(), which do the same jobs but don’t end the output buffer.

We could rewrite the previous script like this:This time the buffer is flushed but left open, then cleaned and still left open, and finally, automatically closed and flushed by PHP as the script ends.

This saves creating and destroying output buffers, which is about 60% faster than opening and closing buffers all the time.

STACKING BUFFERMultiple output buffers can be open simultaneously, in which case, PHP writes to the most recently opened buffer.

For example.

That script will print out “Hello first!.

” The first buffer is started and filled with “Hello first”, then a second buffer is started on top of the previous buffer, leaving the original still intact.

The new buffer is filled with “Hello second”, but ob_clean() is called, clearing the most recent buffer and leaving the first untouched.

The original buffer is then automatically sent by PHP when the script terminates.

Stacking output buffers become more important when you remember that it’s generally smart to make your whole page buffered in a master buffer.

Without stackable buffers, you would be unable to use any other buffers inside the main page.

FLUSHING STACKED BUFFERSWhen you have no output buffer open, any text you print out goes straight to your user.

When you have an output buffer, that text is stored away until you choose to flush it.

When you have stacked output buffers, your buffer flushes data up one level as opposed to going directly to the output.

For example:ob_start();print "In first buffer!.";ob_start();print "In second buffer.";ob_end_flush();print "In first buffer.";ob_end_flush;the output will be the following:In first bufferIn second bufferIn first bufferAs you can see, the second buffer gets flushed into the first buffer where it was left off, as opposed to directly — it literally gets copied into the parent buffer.

Take a look at the following script:ob_start()print "in first buffer.";ob_start();print "In second buffer.";print "In first buffer.";ob_end_clean();It is the same as the previous script, with the only difference being the last line ob_end_clean() is used rather than ob_end_flush().

That script output nothing at all, because the second buffer gets flushed into the first buffer and then the first buffer gets cleaned, which means the client receives none of the text.

As long as you keep in mind that output buffers are stacked, not parallel, this functionality will work in your favour — you can progressively build up your content by opening up new buffers and flushing in content to a parent buffer as you go.

READING BUFFERSOutput buffers are a two-way affair, which means you can read from them as well as write to them.

So far we have only covered writing data; reading that the data back is done by using the ob_get_content() function.

The ob_get_contents() function takes no parameter and returns the full contents of the most recently opened buffer.

For example:$result = mysql_query("SELECT * FROM Employeetable WHERE ID = 55;");while ($row = mysql_fetch_assoc($result)) { extract($row); print "Some info A: $SomeInforA."; print "Some info B: $SomeInforB."; print "Some info C: $SomeInforC."; //.



print "Some infor Z: $SomeInfoZ.";}That script sends its data to the screen.

With output buffering, we can change it to save to a file, like this:ob_start()$result = mysql_query("SELECT * FROM EmployeeTable WHERE ID = 55;");while ($row = mysql_fetch_assoc($result)) { extract($row); print "Some info A: $SomeInforA."; print "Some info B: $SomeInforB. More details

Leave a Reply