Output Buffering

An interesting new feature in version 4 of PHP is output control. Output control (or output buffering) allows you to write and execute your scripts as normal but send data to the Web browser only at select points. The main benefit of this system is that you can call the header(), setcookie(), and session_start() functions at nearly any spot in your script without concern for the headers already sent error messages.

To begin output buffering, use the ob_start() function. Once you call it, every echo(), print(), and similar function will send data to a memory buffer rather than the Web browser. Conversely, HTTP calls (like header() and setcookie()) will not be buffered and will operate as usual.

At the conclusion of the script, ...

Get PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.