Examples of buffers in PHP are detailed

Source: Internet
Author: User
Tags echo date

The PHP buffer is turned on by default, and its default parameter is in the php.ini configuration file with a value of 4096 bytes. In which the output_buffering configuration parameters are found to modify the size of the PHP buffer.

Developers can also ob_start() manually process the PHP buffer mechanism through functions in the script. This way, even if the output exceeds the size of the configuration parameter, the data is not transferred to the browser, the ob_start() PHP buffer space is set to large enough, and only after the execution of the script or calling the function, will it be sent to the ob_end_flush() browser.

We edit the php.ini configuration file, output_buffering modify the values and do the following tests. When output_buffering modified to 4096, output less data so that it is smaller than a PHP buffer. The code is as follows:

for ($i = 0; $i < $i + +) {    echo $i. ' <br/> ';    Sleep ($i + 1);          //}

After execution you will find that it does not have output like regular logic every few seconds, but until the end of the script loop, it will not be output at once. This is the case until the end of the script processing, the browser interface will remain blank, because the amount of data is too small, the output buffer is not full. The order in which the data is written: The Echo statement is output to the PHP buffer, the TCP buffer, and the browser.

Next we modify the output_buffering=0, still output less data, but the actual data is greater than the PHP buffer. The code is as follows:

for ($i = 0; $i < $i + +) {    echo $i. ' <br/> ';    Flush ();  Notify the operating system to the bottom, send the data to the client browser    sleep ($i + 1) as soon as possible;      //}

The result of this script is not consistent with the earlier, because the buffer capacity is set to 0, that is, the PHP buffer mechanism is disabled. At this point we will see intermittent intermittent output in the browser, without having to wait until the script finishes to see the output. This is because the data is not stuck in the output cache. The order in which the data is written is the echo output to the TCP buffer, which is then exported to the browser. We then modify the parameters to output_buffering=4096, and the output data is larger than one buffer. The Ob_start () function is not called in this example. Prepare a 4KB size file or use the DD command to create a file under the shell:

$dd If=/dev/zero of=f4096 bs=4096 count=1

Verify with the following code:

for ($i = 0; $i < $i + +) {    echo file_get_contents ('./f4096 '). $i. ' <br/> ';    Sleep ($i + 1);    }

As you can see, the program response is not finished (the HTTP connection is not closed), you can see the intermittent output, the browser interface will not remain blank. Although the PHP output buffer mechanism is enabled, it still intermittently outputs, rather than a one-time output, because the PHP buffer space is not enough, and every time a buffer is written, the data is sent to the client browser.

As in the previous example, that is output_buffering=4096 , the output data is larger than a PHP buffer. This time we invoke ob_start() the code as follows:

Ob_start ();      Open PHP buffer for ($i = 0; $i < $i + +) {    echo file_get_contents ('./f4096 '). $i. ' <br/> ';    Sleep ($i + 1);} Ob_end_flush ();

Wait until the server-side script finishes processing, and the response ends before you see the full output. The output interval is short enough to feel the pause. Before the output, the browser remains blank and waits for server-side data. This is because, once PHP has called the ob_start() function, it expands the PHP buffer to be large enough to ob_end_flush send the data in the PHP buffer to the client browser until the function call or the end of the script runs.

ob_startActivation output_buffering mechanism. Once activated, the script is no longer output directly to the browser, but is temporarily written to the PHP buffer. PHP is enabled by default output_buffering , by calling ob_start() functions to output_buffering extend the value to large enough. You can also $chunk_size specify output_buffering a value by using the. The $chunk_size default value is 0, which means that data in the PHP buffer is not sent to the browser until the end of the script run. If the size is set $chunk_size , the data in the buffer is sent to the browser as long as the data length in the buffer reaches that value.

$ouput_callbackdata in the PHP buffer can be processed by specifying parameters, such as functions ob_gzhandler() , which compress the data in the buffer and then pass it to the browser.

ob_get_contents()The function is to get a copy of the data in a PHP buffer, which is an important function. Take a look at the following example:

<?phpob_start ();   ? >

After running the above script, look at the source code, there will be two paragraphs of the same HTML, the latter is through ob_get_contents() the function to get the contents of the buffer.

ob_end_flush()Both ob_end_clean() functions will turn off the output buffer.

The difference is that ob_end_flush() only the data in the PHP buffer is sent to the client browser, and the ob_clean_clean() data in the PHP buffer is deleted but not sent to the client.

ob_end_flush()After the call, the data in the PHP buffer still exists and ob_get_contents() can still get copies of the data in the PHP buffer.

Related Article

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.