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. Then when the buffer is full, PHP automatically sends the 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 my statement below), this buffer is controlled by the Ob_ series function, that is, PHP does not maintain its own buffer, 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.
3 |
echoob_get_contents() ; |
Just like the code above, it doesn't make any sense.
I think about it, and then search the Internet, and found that quite a few beginners (technical beginners, not necessarily the first year of PHP, some people life is a beginner), do not understand the role of OB, but the network often called the OB output buffer, output cache, So quite a lot of people take OB series functions as a tool to speed up the display of PHP pages.
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 it, 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 modified under my friend's code, you can achieve the gzip compression of the page.
1 |
ob_start(ob_gzhandler); |
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.
Go Introduction to the PHP Ob_start () function