Home  >  Article  >  Backend Development  >  Introduction to php ob cache and detailed explanation of ob function

Introduction to php ob cache and detailed explanation of ob function

伊谢尔伦
伊谢尔伦Original
2018-05-29 10:24:585811browse

ob cache introduction

ob is the abbreviation of output buffering, output buffer, and the buffer is controlled through the output_buffering variable in php.ini. Its default value is off and can be set to on to open the buffer. After calling the buffer, even if the ob function is not used in the program, the code actually uses the buffer. In addition, regardless of the setting of output_buffering in php.ini, php in cli mode is always turned off by default. Why a buffer? To put it simply, the high-speed CPU has processed its own data early and wants to transmit it to the user through the line, but the line is too narrow and cannot be transmitted at once. If a buffer is introduced, the CPU can quickly put the generated data into the buffer, and then rest somewhere cool. The buffer outputs data in a timely manner according to instructions. This effectively solves the contradiction between high-speed CPU and low-speed I/O devices.

Basic principles of ob: If the ob cache is turned on, the echo data is first placed in the ob cache. If it is header information, it is placed directly in the program cache. When the page is executed to the end, the ob cached data will be placed in the program cache, and then returned to the browser in turn.

The basic function of ob:

1) Prevent the use of setcookie(), header() or session_start() to send header files after the browser has output Error caused by function. In fact, it is better to use this kind of usage less often and develop good coding habits.
2) Capture the output of some unobtainable functions. For example, phpinfo() will output a lot of HTML, but we cannot use a variable such as $info=phpinfo(); to capture it. At this time, ob will be useful. .
3) Process the output content, such as gzip compression, conversion between Simplified and Traditional Chinese, and some string replacement.
4) Generating static files is actually capturing the output of the entire page and then saving it as a file. Often used in HTML generation or full page caching.

Detailed explanation of ob related functions

1. Flush: refresh the contents of the buffer and output.
Function format:

flush()

Description: This function is frequently used and is very efficient.
2. ob_start: Open the output buffer
Function format:

void ob_start(void)

Description: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in Internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
3, ob_get_contents: Return the contents of the internal buffer.
Usage:

string ob_get_contents(void)

Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
4. ob_get_length: Returns the length of the internal buffer.
Usage:

int ob_get_length(void)

Description: This function will return the length in the current buffer; the same as ob_get_contents, if the output buffer is not activated. then returns FALSE.
5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage:

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
Usage:

void ob_end_clean(void)

Description: This function will not output the contents of the internal buffer but delete it!
7. ob_implicit_flush: Turn on or turn off absolute flush
Usage:

void ob_implicit_flush ([int flag])

Description: Anyone who has used Perl knows the meaning of $|=x. This string can open/close the buffer. , and the ob_implicit_flush function is the same as that one. The default is to close the buffer. After turning on absolute output, each script output is sent directly to the browser, and there is no need to call flush()

The example code of the flush function is as follows:

Description: flush() is a very efficient function. Its very useful function is to refresh the cache# of browser ##.

About the example code of the ob series function:

For example, you can use the setting information of the server and the client, but this information will be different depending on the client. , what if you want to save the output of the phpinfo() function? Before there was no buffer control, it can be said that there was no way at all, but with buffer control, we can easily solve it.

Using the above method, you can save the phpinfo information of different users. This may not have been possible before! In fact, the above is a method to convert some "processes" into "functions"!

About the static template output example code:

The so-called static template technology is to use a certain method to make the user get the html page generated by PHP on the client side. . If this html page will no longer be updated, then 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, technology like this will bring The benefits are huge.

【Related tutorial recommendations】

1. "

php.cn Dugu Jiujian (4)-php video tutorial"

2. A complete set of tutorials on PHP programming from entry to master

The above is the detailed content of Introduction to php ob cache and detailed explanation of ob function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn