PHP output buffer control (OutputControl) _ PHP Tutorial

Source: Internet
Author: User
PHP output buffer control (OutputControl) learning. PHP output buffer control (OutputControl) learning php buffer introduction in fact, I still have a vague impression on the phpob series, but I still don't know much about how to play it. curd, indeed, the Output Control (Output Control) of these PHP outputs is learned.
Introduction to php buffer

In fact, I still have a vague impression on the php ob series, but I still don't know much about how to play it. curd does not go deep into these contents at ordinary times. As a phper, I am very ashamed. Network

I searched a channel, copied each other, and the code run cannot be described by the Author. This article is produced by conscience, and the code has been run by the author.

When the output is executed, such as echo and print. The output is not sent to the web server immediately. Instead, the data is written to the php buffer. Php output_buffering mechanism

The benefits certainly improve performance. In fact, the php file is finally displayed on the browser and goes through three buffer stages: php buffer = "web server buffer =" browser buffer. Final Display

Display in browser

By default, php buffer is enabled, and the default value of this buffer is 4096, that is, 4 kb. You can find the output_buffering configuration in the php. ini configuration file.

. Buffer is a memory address space. the default size of a Linux system is generally 4096 (4 KB), that is, a memory page. It is mainly used for devices with different storage speeds or devices with different priorities.

The region where data is transferred. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example. when you open a text editor and edit a file

Every time you enter a character, the operating system will not immediately write the character directly to the disk, but first write it to the buffer. when it is full of a buffer, the buffer will be

When calling the kernel function flush (), the dirty data in the buffer must be written back to the disk.

For example

Echo "South Africa Amitabha
";

Header ("content-type: text/html; charset = 'utf-8 '");

Echo "really cool! ";

// Output

// Nanwu Amitabha

// Really cool

Header () must be called before any actual output, but our program has been output but runs normally. Check the following code:

Echo "South Africa Amitabha
";

Ob_flush ();

Header ("content-type: text/html; charset = 'utf-8 '");

Echo "really cool! ";

// Output

// Nanwu Amitabha

// Cannot modify header information-headers already sent by (output started at E: \ php \ test. php: 3)

// Really cool

The above program indicates that the program is not output immediately, but the buffer and output are refreshed only when the ob_flush function is called.

Ob_flush () and flush ()

The ob_flush () and flush () function php manuals are described in detail. you can check them out. The difference between the two is:

Ob_flush () is the buffer for refreshing PHP itself

Flush () is the buffer used to refresh the WebServer server. Output to the browser. However, the following situations may occur:

Some web server programs, especially the web server programs under Win32, will cache the script output until the program ends before sending the results to the browser.

Some Apache modules, such as mod_gzip, May output the cache by themselves, which will cause the results produced by the flush () function to be not immediately sent to the client browser.

Even the browser caches the received content before it is displayed. For example, the Netscape browser caches content before receiving a line break or the beginning of an html tag, and receives

The entire table is not displayed until it is marked.

In some versions of Microsoft Internet Explorer, the page is displayed only after 256 bytes are received. Therefore, some extra spaces must be sent for these browsers

Display the page content.

For example:

/**

Server: LightTPD/1.4.28 (Win32)

X-Powered-By: PHP/5.3.27

*/

Echo 'ffa boundless '."
";

Ob_flush ();

Flush ();

Sleep (1 );

Echo 'always go ';

// Output

The code above is a line of output in the chrome browser, and all output in the ie series browser. This is actually the fourth article above. some browsers only receive 256

Starts to display. Change the code above to the following format:

/**

Server: LightTPD/1.4.28 (Win32)

X-Powered-By: PHP/5.3.27

*/

Echo str_pad ('', 240)." \ n ";

Echo 'ffa boundless '."
";

Ob_flush ();

Flush ();

Sleep (1 );

Echo 'always go ';

// Output

In this way, a line of output is displayed under ie, because it contains more than 256 characters.

Description of other ob functions

1. ob_end_flush and ob_end_clean

As the name suggests, end ends. when the buffer is closed, the output buffer is closed, and the output buffer is cleared. For example

/**

Server: LightTPD/1.4.28 (Win32)

X-Powered-By: PHP/5.3.27

*/

Echo 'before ';

Ob_end_clean ();

Echo str_pad ('', 4096)." \ n ";

For ($ I = 10; $ I> 0; $ I --)

{

Echo $ I;

Sleep (1 );

}

The above code is to output all the content, rather than output one by one. Ob_end_clean () is the buffer disabled? Why not output one by one? as we mentioned above,

Php is not directly output to the browser, but to the web server. Although php has no buffer. However, there are still some web servers. So the following code:

/**

Server: LightTPD/1.4.28 (Win32)

X-Powered-By: PHP/5.3.27

*/

Echo 'before ';

Ob_end_clean ();

Echo str_pad ('', 4096)." \ n ";

For ($ I = 10; $ I> 0; $ I --)

{

Flush ();

Echo $ I;

Sleep (1 );

}

With flush (), a row of output is displayed. If you replace ob_end_clean with ob_end_flush, before is output.

For more information about other functions, see the manual.

Summary

Php scripts must go through php buffer = web server buffer = browser buffer. Finally, it is displayed in the browser. Indispensable. So we want

Ob_flush and flush and echo str_pad ('', 4096) can be used to debug the desired effect.

Http://www.bkjia.com/PHPjc/928279.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/928279.htmlTechArticlePHP Output buffer Control (Output Control) learning php buffer introduction in fact, I'm still very vague on the php ob series, how to play, not very familiar with, usually curd, indeed for these...

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.