Conversion tables A-K
  Conversion tables L-Z
  SPAM database query
  Reverse WHOIS utility
  North America area codes
  PHP Example forums

PHP example repository.

Web www.web-max.ca
Back to examples
Using output buffers

Output buffering allows you to control how data is sent to the browser. It also allows you to capture data before it is sent. This functionality gives you the ability, for example, to create static versions of dynamic pages ( maybe to save on database access ) or to send a HTML page via email.

To use output buffers to save a page, you will need three functions:

ob_start: To begin capturing the output
ob_get_contents: To get the contents in a variable
ob_end_flush: To send the data to the browser

The example below is a very simple use. Remember, you can put the functions in the middle of your PHP scripts to capture all or part of your output.

<html>
<body>
<b>Thanks for your order the details are as follows:</b>
<?php

// Start to capture the output

    
ob_start();

?>
<table>
<tr><td>Order number</td><td>12321</td></tr>
<tr><td>Shipping date</td><td>22/11/1972</td></tr>
</table>
<?php

//    get the contents of the buffer into a variable

    
$content ob_get_contents();
    
// In this example send the content in an email - 
// in reality this is not a very efficent way of 
// sending the email in the middle of the script,
// but it will work for this example

    
$HTML         $content;
    
$from         "test@test.com";
    
$to           "this@that.com";
    
$subject     "Order confirmation";

    
sendHTMLemail($HTML,$from,$to,$subject);    

// flush the data to the browser

    
ob_end_flush();

?>
<b>To return to the order page click here</b>
</body>
</html>
<?php

// Example 

$HTML         "<b>This is a test</b>";
$from         "test@test.com";
$to           "this@that.com";
$subject     "I'm sending a test HTML email";

// SendHTMLemail from example library

sendHTMLemail($HTML,$from,$to,$subject);

?>