Explain PHP's ob_start () function _ PHP Tutorial

Source: Internet
Author: User
Explain PHP's ob_start () function. Phpob_start and ob_end_flush () are the buffer output functions of php. Ob_start ([stringoutput_callback])-when the output buffer is opened, all output information is not directly sent to the browser. php ob_start and ob_end_flush () are the buffer output functions of php.

Ob_start ([string output_callback])-open the output buffer. all output information is not directly sent to the browser, but saved in the output buffer. an optional callback function is used to process output result information.

Ob_end_flush-end (SEND) output buffer content, disable the output buffer.

Php outputs will be stored in a memory maintained by php, which is also known as buffer and cache. Then, when the buffer is full, php will automatically send the data to the web server.

That is to say, each echo does not necessarily output anything, but is saved in the buffer.

Ob_start () can be understood as (but it is actually different from the following statement). This buffer is controlled by the ob _ series functions, that is, PHP will not maintain its own buffer, and will not automatically send the buffer content to the web server until you ob_end () or similar ob operations.

The ob _ function is generally used to capture the current output, which has nothing to do with efficiency. There are many reasons for capturing the output. for example, if I capture the output and cache it into a file, the content of the cache file can be directly read as the output in the next request.

Ob_start (); echo ob_get_contents ();

This is the code similar to the above. to put it bluntly, there is no meaningful code.

After thinking about it carefully, I searched the internet and found that quite a few beginners (technically beginners, not necessarily the first year of PHP, and some people are beginners all their lives ), I don't understand the role of ob, but I often call ob an output buffer and an output cache on the internet. so many people regard ob functions as a tool to accelerate the display of PHP pages.

In fact, ob is short for output buffering, rather than output cache. ob's correct use can help speed. However, blindly adding ob functions will only increase the extra CPU burden. Next I will talk about the basic functions of ob.

  1. Prevent the use of setcookie or header or session_start function after the browser outputs. (I thought that the code I first talked about was such a function, but later my friend said it was not.) in fact, it is better to use it less and develop good code habits.
  2. Capture the output of some unrecoverable functions. for example, phpinfo will output a lot of HTML, but we cannot use a variable such as $ info = phpinfo (); to capture, at this time, ob will work.
  3. Process the output content, such as gzip compression, simple conversion, and replacement of some strings.
  4. Generating static files is actually capturing the output of the entire page and saving it as a file, which is often used in generating HTML or the whole page cache.

For GZIP compression in the third point just mentioned, it may be something that many people want to use, but it is not actually used. In fact, by slightly modifying the code of my friend, you can implement gzip compression on the page.

Ob_start (ob_gzhandler); content

Yes, you can add the ob_gzhandler callback function. However, there are some minor issues. One is that zlib is required, and the other is that the browser does not determine whether gzip is supported. (currently, it seems that all of them are supported, the iphone browser seems to be supported ).

In the past, we used to determine whether the browser supports gzip, use a third-party gzip function to compress the content of ob_get_contents (), and finally echo the content.

Http://www.bkjia.com/PHPjc/752507.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752507.htmlTechArticlephp ob_start and ob_end_flush () are php buffer output functions. Ob_start ([string output_callback])-open the output buffer. all output information is not directly sent to the browser,...

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.