Introduction to the PHP Ob_start () function
PHP Ob_start and Ob_end_flush () are the buffered output functions of PHP.
Ob_start ([string output_callback])-Opens the output buffer, all output information is not sent directly to the browser, but is stored in the output buffer, the optional callback function is used to process the output information.
Ob_end_flush– ends (sends) the contents of the output buffer and closes the output buffer.
PHP output, will be saved in a PHP maintenance memory, called buffer is also OK, cache is OK, is a meaning. And then when this buffer is full,
PHP will automatically send this data to the Web server.
This means that each time echo does not necessarily output something, it is stored in buffer.
The meaning of Ob_start () can be understood as (but actually differs from what I'm saying below), which is controlled by the Ob_ series function, which is,
PHP does not maintain its own buffer and does not automatically send the contents of the buffer automatically to the Web server until you ob_end () or a similar OB operation.
The Ob_ function is generally used to capture the current output, which is not related to efficiency. As for why the capture output, for example, I capture the output, cached in a file, the next request can be directly read the contents of the cache file as output.
Ob_start ();
Content
Echo ob_get_contents ();
Just like the code above, it doesn't make any sense.
I thought about it, and then searched the Internet, found that quite a few beginners, do not understand the role of OB, but the network often called the OB output buffer, output cache, so a considerable number of people to the OB series functions as a way to speed up the PHP page display tool.
In fact, OB is the abbreviation for output buffering, rather than output cache,ob with the right to be able to help speed, but blind plus OB function, will only increase the CPU extra burden. Below I talk about the basic role of OB.
Prevents the use of Setcookie after the browser has output, or errors caused by the Header,session_start function. (I thought the code was the first to say that the role, but later friends said it is not), in fact, such a usage of less use for good, develop good code habits.
Capturing the output of some unreachable functions, such as phpinfo, will output a whole bunch of HTML, but we can't use a variable, such as $info=phpinfo (), to capture, and the OB works.
Processing of output content, such as gzip compression, for example, for simple conversion, such as some string substitution.
Generating a static file is essentially capturing the output of the entire page and then saving it to a file that is often used in generating HTML or full-page caches.
For the 3rd in the gzip compression, may be a lot of people want to use, but there is no real use, in fact, slightly modify the code, you can achieve the gzip compression of the page.
Ob_start (Ob_gzhandler);
Content
Yes, add a Ob_gzhandler This callback function is OK, but this is a little problem, one needs zlib support, two is not to determine whether the browser supports gzip (now seems to support, the iphone browser seems to support).
The previous practice was to determine if the browser supports gzip, and then use the third-party gzip function to compress the contents of the ob_get_contents (), and finally echo.
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.