Introduction to the PHP OB cache and an explanation of OB functions

Source: Internet
Author: User
Tags file info phpinfo
OB Cache Introduction

The OB is the abbreviation for output buffering, and the buffer is controlled by the output_buffering variable in the php.ini. The default value is off, which can be set to on to open buffer. After hitting buffer, even if the program does not use the OB function, the code is actually using the buffer. In addition, PHP is always turned off by default in CLI mode, regardless of the output_buffering settings in php.ini. Why, what if the buffer zone? In short, the high-speed CPU processing their own data early, want to pass the line to the user, but the line is too narrow, the delivery is not over. If a buffer is introduced, the CPU can quickly put the generated data into a buffer, and then where it is cool where to stay and rest. Buffers output data in a timely manner according to the instructions. This kind of a reasonable solution to the high-speed CPU and low-speed I/O equipment contradictions.

the basic principle of OB : If the OB cache is open, then Echo's data is first placed in the OB cache. If it is header information, put it directly in the program cache. When the page executes to the end, the data of the OB cache is placed in the program cache and returned to the browser in turn.

the basic role of OB :

1) Prevent errors caused by functions such as Setcookie (), header (), or session_start () to send header files after the browser has output. In fact, this usage is less good, develop good code habits.
2) 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.
3) The content of the output is processed, such as gzip compression, for example, for simple conversion, for example, some string substitution.
4) Generate static files, in fact, capture the entire page of output, and then save as a file. Often used in generating HTML, or full-page caching.

An explanation of OB correlation functions

1, Flush: Flush the contents of the buffer, output.
function format:

Flush ()

Description: This function is often used and is highly efficient.
2. Ob_start: Open Output buffer
function format:

void Ob_start (void)

Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.
3. Ob_get_contents: Returns the contents of the internal buffer.
How to use:

String ob_get_contents (void)

Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.
4. Ob_get_length: Returns the length of the internal buffer.
How to use:

int ob_get_length (void)

Note: This function returns the length of the current buffer, as with ob_get_contents, if the output buffer is not activated. FALSE is returned.
5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer.
How to use:

void Ob_end_flush (void)

Description: This function sends the contents of the output buffer (if any).
6. Ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
How to use:

void Ob_end_clean (void)

Description: This function does not output the contents of the internal buffer but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use:

void Ob_implicit_flush ([int flag])

Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same, the default is to close the buffer, open the absolute output, each script output is sent directly to the browser, no longer need to call flush ()

The code for the Flush function instance is as follows:

<?phpfor ($i = 1; $i <=; $i + +) print ("");//This sentence is very critical, the structure of the cache so that its content only to reach a certain size to output from the browser in other words, if the cache content does not reach a certain size, It is not output until the program has finished executing. After//tests, it was found that the bottom limit of this size is 256 characters. This means that content received by the cache will be sent out continuously. for ($j = 1; $j <=, $j + +) {echo $j. "; Flush (); This will cause the cache additions to be squeezed out and displayed to the browser sleep (1); Let the program "Sleep" a second, will let you see the effect more clearly}?>

Description: Flush () is a highly efficient function, and its very useful function is to refresh the browser cache.

Example code for OB series functions:

For example, you use the server and client settings information, but this information is different from the client, if you want to save the Phpinfo () function output what to do? Before there is no buffer control, it can be said that there is no way, but with the control of the buffer, we can easily solve.

<?phpob_start (); Open buffer phpinfo (); Use the Phpinfo function $info=ob_get_contents (); The contents of the buffer zone are obtained and assigned to $info$file=fopen (\ ' info.txt\ ', \ ' w\ '); Open File Info.txtfwrite ($file, $info); Write information to Info.txtfclose ($file); Close File info.txt?>

Using the above method, you can save the Phpinfo information of different users, which in the past I am afraid there is no way to do! In fact, the above is a few "process" into the "function" Method!

About the static template output instance code:

<?phpob_start ()///open buffer//php All output of the page $content = ob_get_contents ();//Get the full contents of the PHP page output $fp = fopen ("output00001.html", "W"); Create a file and open, ready to write Fwrite ($fp, $content); Write the contents of the PHP page to output00001.html, then ... fclose ($FP);? >

The so-called static template technology is in some way, so that users on the client side is generated by PHP HTML page. If this HTML page will not be updated again, then when another user browses this page again, the program will no longer call PHP and related databases, for some of the more informative sites, similar to the benefits of this technology is very large.

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. PHP programming from getting started to mastering the full set of tutorials

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.