Output Buffering PHP

As stated above, the Web server buffers content sent to the browser, and you can request that the buffer be flushed. PHP4 introduced a new mechanism for buffering output you can control completely. Four functions control PHP's output buffer: ob_start, ob_end_flush, ob_end_clean, and ob_ get_contents.

When you call the ob_start function, anything you send to the browser is placed into a buffer. This includes text outside of PHP tags. The Web server will not receive this content until the ob_end_flush function is called. There are several powerful applications of these functions. One is to avoid the problem associated with sending headers. Because all headers are sent at once, before any content, you have to take care when using the header function. This results in a script design where early parts of a script are declared a "no output" zone, which can be annoying. If you use output buffering, you can safely add headers to the stack where you wish, and delay sending content until the last line of your script.

Another application of these functions is in building HTML tables. Imagine creating a table filled with data from a database. You first print the opening tags for the table. You execute a query and loop over the results being returned. If everything executes without error, you print a closing table tag. If an error occurs within the loop, you may have to abort, and the code that closes the table is never reached. This is bad because of the behavior of Netscape Navigator: It won't display information inside an unclosed table. The solution is to turn on output buffering before assembling the table. If assembly completes successfully, you can flush the buffer. Otherwise you can use ob_end_clean, which throws away anything in the buffer.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics