Page 106 - Open Soource Technologies 304.indd
P. 106

Unit 6: Building Blocks of PHP



            6.1.8 Browser Output                                                                  Notes

            The PHP output buffering functions provide a handy way of intercepting the contents of the
            buffer before it is sent to the browser. The output is whatever is sent to the browser whenever
            you print something off. PHP allows you to capture this output in a buffer before it is sent to
            the browser.

            The Output Control functions allow you to control when output is sent from the script. This can
            be useful in several different situations, especially if you need to send headers to the browser
            after your script has began outputting data. The Output Control functions do not affect headers
            sent using header() or setcookie(), only functions such as echo() and data between blocks of
            PHP code.
            Example #1 Output Control example

            <?php

            ob_start();
            echo “Hello\n”;

            setcookie(“cookiename”, “cookiedata”);

            ob_end_flush();
            ?>

            In the above example, the output from echo() would be stored in the output buffer until ob_
            end_flush() was called. In the mean time, the call to setcookie() successfully stored a cookie
            without causing an error. (You can not normally send headers to the browser after data has
            already been sent.)
            Browser Output Control Functions

               •  flush — Flush the output buffer
               •  ob_clean — Clean (erase) the output buffer
               •  ob_end_clean — Clean (erase) the output buffer and turn off output buffering

               •  ob_end_flush — Flush (send) the output buffer and turn off output buffering
               •  ob_flush — Flush (send) the output buffer
               •  ob_get_clean — Get current buffer contents and delete current output buffer

               •  ob_get_contents — Return the contents of the output buffer
               •  ob_get_flush — Flush the output buffer, return it as a string and turn off output buffering
               •  ob_get_length — Return the length of the output buffer

               •  ob_get_level — Return the nesting level of the output buffering mechanism
               •  ob_get_status — Get status of output buffers
               •  ob_gzhandler — ob_start callback function to gzip output buffer

               •  ob_implicit_flush — Turn implicit flush on/off
               •  ob_list_handlers — List all output handlers in use



                                             LOVELY PROFESSIONAL UNIVERSITY                                   101
   101   102   103   104   105   106   107   108   109   110   111