Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Stacking Buffers

Multiple output buffers can be open simultaneously, in which case, PHP writes to the most recently opened buffer. For example:

    ob_start();
    print "Hello first!\n";

    ob_start();
    print "Hello second!\n";

    ob_clean();

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 (though just out of reach for the time being). 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 becomes 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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page