In-depth usage of phpob_start

Source: Internet
Author: User
In-depth usage of phpob_start

  1. Ob_start (); // open the buffer
  2. Echo \ "Hellon \"; // output
  3. Header ("location: index. php"); // redirects the browser to index. php.
  4. Ob_end_flush (); // output all content to the browser
  5. // By bbs.it-home.org
  6. ?>

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!

1. related functions: 1. Flush: refresh the buffer content and output it. Function format: flush () Description: This function is frequently used and highly efficient. 2. ob_start: format of the function used to open the output buffer: void ob_start (void) description: When the buffer is activated, all non-file header information from the PHP program is not sent, but saved in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content. 3. ob_get_contents: returns the content of the internal buffer. Usage: string ob_get_contents (void) Description: This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned. 4. ob_get_length: return the length of the internal buffer. Usage: int ob_get_length (void) Description: This function returns the length of the current buffer. it is the same as ob_get_contents if the output buffer is not activated. Returns FALSE. 5. ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer. Usage: void ob_end_flush (void) Description: This function sends the content of the output buffer (if any ). 6. ob_end_clean: delete the content of the internal buffer and disable the use of the internal buffer. void ob_end_clean (void): This function will not delete the content of the internal buffer! 7. ob_implicit_flush: How to enable or disable absolute refresh: void ob_implicit_flush ([int flag]) description: Perl users all know the meaning of $ | = x, this string can enable/disable the buffer, while the ob_implicit_flush function is the same as that. by default, the buffer is disabled. after the absolute output is opened, the output of each script is directly sent to the browser without calling flush ()

2. In-depth understanding:

1. about the Flush function: This function appears in PHP3, which is a very efficient function. it has a very useful function that is to refresh the browser cache. we will give an example with very obvious running effect to illustrate flush. example 2.

  1. For ($ I = 1; $ I <= 300; $ I ++) print ("");
  2. // This sentence is critical. the cache structure makes it output from the browser only when the content reaches a certain size.
  3. // In other words, if the cache content does not reach a certain size, it will not be output before the program is executed. Jing
  4. // After testing, I found that the base size is 256 characters long. This means that all content received by the cache will be
  5. // Continuously sent out.
  6. For ($ j = 1; $ j <= 20; $ j ++ ){
  7. Echo $ j ."
  8. ";
  9. Flush (); // This part will squeeze out the new content of the cache and display it on the browser.
  10. Sleep (1); // let the program "sleep" for one second, so that you can see the effect more clearly
  11. }
  12. ?>

Note: If you add ob_implicit_flush () to the program header to enable absolute refresh, you can stop using flush () in the program. The advantage of doing this is to improve efficiency!

2. about ob functions: I would like to first reference an example of my good friend y10k: Example 3. for example, you can obtain the configuration information of the server and the client, but this information may vary depending on the client. what if you want to save the output of the phpinfo () function? There is no way to control the buffer, but with the control of the buffer, we can easily solve the problem:

  1. Ob_start (); // open the buffer
  2. Phpinfo (); // use the phpinfo function
  3. $ Info = ob_get_contents (); // Obtain the buffer content and assign it to $ info
  4. $ File = fopen (\ 'info.txt \ ', \ 'W \'); // open the info.txt file.
  5. Fwrite ($ file, $ info); // write the information to info.txt
  6. Fclose ($ file); // Close the info.txt file
  7. ?>

With the above method, you can save the phpinfo information of different users. I am afraid there is no way to do this before! In fact, the above is to convert some "process" into "function" methods! Someone may ask, "is it like this? Is there any other purpose? "Of course, for example, the PHP syntax highlighted in the forum of the author is related to this. (the default PHP syntax highlighted in the display function will be directly output and the results cannot be saved, I am afraid it will be a waste of CPU resources if it is displayed in every call. in my forum, I will keep the result of the function highlighted by the syntax in the buffer control method.) if you are interested, let's take a look.

You may have some knowledge about the functions of ob_start (). the above example seems simple, but you have mastered the key points of using ob_start. <1> use ob_start to open the browser cache. This ensures that the cache content will not be output before you call flush (), ob_end_flush () (or after the program is executed. <2>. now you should know your advantages: you can use header, setcookie, and session after any output content, which is a great feature of ob_start. you can also use the ob_start parameter, after the cache is written, run the command automatically, such as ob_start (\ "ob_gzhandler \"). The most common practice is to use ob_get_contents () to get the content in the cache, and then proceed ...... <3>. after processing is completed, we can use various methods for output, flush (), ob_end_flush (), and automatic output after the program is executed. Of course, if you use ob_get_contents (), you can control the output mode by yourself.

I. static template technology

Introduction: the static template technology allows users to obtain html pages generated by PHP on the client side in some way. If this html page is no longer updated, when another user browses this page again, the program will no longer call PHP and related databases. for some websites with a large amount of information, for example, sina, 163, sohu. The benefits of similar technologies are enormous.

12. Next page

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.