Miscellaneous

  Home arrow Miscellaneous arrow Page 2 - PHP Output Buffering
MISCELLANEOUS

PHP Output Buffering
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2003-06-10

    Table of Contents:
  • PHP Output Buffering
  • How It's Done
  • Callback Functions
  • Examples
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    PHP Output Buffering - How It's Done


    (Page 2 of 5 )

    The Basics

    We begin with a simple Hello World script.

    <?php
    // Start buffering
    ob_start();
    print 
    "Hello World";

    // Grab the buffer
    $buffer ob_get_contents();

    // Stop buffering
    ob_end_clean();

    // Display the buffer
    print "'$buffer'";
    ?>

    The output isn't all that exciting, simply Hello World in single quotes. The function ob_start() initiates the output buffering. "Hello World" is then printed, but it does not get sent to the client. Instead it is buffered until we do something with it. That we do by storing the buffer in $buffer by calling ob_get_contents(). ob_end_clean() ends the output buffering, which means that the last print command displays to the client.

    Nested Output Buffering

    You might be asking what happens if you call ob_start() twice without closing the buffer. PHP's real smart, so it handles it very nicely by nesting the buffers. The following script shows how this is done.

    <?php
    // Open buffer #1
    ob_start();
    print 
    "Line 1\n";

    // Open buffer #2
    ob_start();
    print 
    "Line 2\n";

    // Grab the contents of buffer #2
    $buf2 ob_get_contents();

    // Close buffer #2
    ob_end_clean();
    print 
    "Line 3\n";

    // Grab the contents of buffer #1
    $buf1 ob_get_contents();

    // Close buffer #1
    ob_end_clean();

    // Output the buffer contents
    print $buf1;
    print 
    $buf2;
    ?>

    Without knowledge of output buffering, one would expect the lines to be printed in numerical order. This is not the case though. After printing line 1, a second buffer is opened which prints line 2 and captures the buffer. Line 3 is then printed in the first buffer. The first buffer captures lines 1 and 3 while the second captures line 2.

    What all this means is that you can build functions and scripts with output buffering without worrying about fouling up an algorithm nested below or above in the logic. As long as you always remember to close every buffer you open, you won't have any problems.

    More Miscellaneous Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap