Use PHP's ob_start () to control your browser cache and generate html

Source: Internet
Author: User
The OutputControl function allows you to control the output of data in the script. It is very useful, especially when you want to output the file header after the data has been output. The Output Control function does not use header () or setcookie (). the Output Control function generates the file header information to allow you to Control the data Output in the script. It is very useful, especially when you want to output the file header after the data has been output. The output control function does not affect the header information sent using header () or setcookie (). it only applies to data blocks similar to echo () and PHP code.

Let's take a simple example to give you a general impression on Output Control:

Example 1.

Bytes -----------------------------------------------------------------------------------------------------------


Ob_start (); // open the buffer
Echo "Hello \ n"; // output
Header ("location: index. php"); // redirects the browser to index. php.
Ob_end_flush (); // output all content to the browser
?>

Bytes -----------------------------------------------------------------------------------------------------------

Anyone who knows about the header () function knows that this function will send a file header to the browser, but if any output (including null output, for example, a blank space, carriage return, or line feed will prompt an error. If we remove ob_start () from the first line and execute this program, we will find an error message: "Header had all ready send "! However, when ob_start is added, no error will be prompted because when the buffer zone is opened, the characters after echo will not be output to the browser, but will be retained on the server until you use flush or ob_end_flush, so there will be no file header output errors!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.