Using Output Buffering With PHP

56
rate or flag this page

By johnathanhebert


Quick Overview of PHP Output

When you write a web page with PHP, the output of the PHP script is what eventually is sent to the client's browser. You probably already knew that, but what you may not know is how and when it is sent.

As the PHP interpreter parses and executes your script, there may be some static HTML markup as well as PHP echo statements that it encounters and processes along the way. In a normal script these are essentially sent to the browser in the order they are processed, but not before the HTTP headers are sent. The HTTP headers are not visible on the web page, but they contain important information for the browser regarding caching, cookies, and lots of other things.

So, in a nutshell, the server receives a request from a browser, assembles and sends HTTP headers, then sends the HTML back piece by piece as it is output by the PHP script. The code below shows a very simple PHP script that outputs an HTML page to display the current date. That is fine for simple pages, but what if you want to alter the HTTP headers in the middle of your script?

<html>
<head><title>PHP Output Buffering - Off</title></head>
<body>
<?php
echo "Today's date is: " . date("d-F-Y");
?>
</body>
</html>

PHP Output Buffering Functions

With PHP you can control the way output is sent to the requesting browser. With the output buffering functions you can tell your script to store all output in the PHP output buffer until you specifically tell it to send it the browser. When you send it to the browser, it is called flushing the output buffer.

To turn on output buffering, just call the function ob_start(). From that point forward in your script, nothing will be sent to the browser until you call one of the output flushing functions. Additionally, if you plan to change HTTP headers somewhere in the middle of your script, make sure you call ob_start() as the first line in your script. That way, none of the headers will be sent until you tell it.

The code below shows how you can use output buffering to send a Cache-Control HTTP header using the PHP header() function, even after you have output some HTML with your script. Once you set the header, you can call ob_end_flush() to flush the output buffer (which means send everything in the buffer to the browser) and end output buffering in your script. If you simply want to flush the buffer but leave on output buffering for the remainder of your script, use ob_flush() instead.

You can read the PHP manual to learn more about output buffering.

<?php ob_start(); ?>
<html>
<head><title>PHP Output Buffering - On</title></head>
<body>
<?php
echo "Today's date is: " . date("d-F-Y");
header("Cache-Control: no-cache");
ob_end_flush();
?>
</body>
</html>

Print   —   Rate it:  up  down  flag this hub

RSS for comments on this Hub

Joseph Montanez  says:
5 months ago

Use output buffering wisely. If there is too much in a buffer, you'll get blank flushes and you can ask a buffer to hold more but then you might hit your sever's php ini memory limit and again lose the buffer.

johnathanhebert profile image

johnathanhebert  says:
5 months ago

I agree... this is just a very simple explanation of what it is. It is not appropriate in all cases -- for example, if you are sending some binary output to the browser, you probably just want to send it byte by byte as it is read.

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working