Details of the use of php buffer output_buffering

  • 2020-06-12 08:42:29
  • OfStack

buffer
buffer is 1 memory address space, and the default size of Linux system is usually 4096(4kb), i.e. 1 memory page. An area used to store data between devices with asynchronous speeds or different priorities. With buffer, processes can be made to wait for each other less. Here said a popular example of 1 point, you open a text editor to edit a file, every time you enter a character, the operating system does not immediately put the characters written to the disk directly, but to write to buffer, when with a buffer will put the data in a buffer written to disk, of course, when the kernel function flush (), forced to write the dirty data in buffer back to disk.

Similarly, when echo,print is executed, the output is not immediately displayed via tcp to the client browser, but written to php buffer. php output_buffering mechanism means that before tcp buffer, a new queue is created through which data must pass. When 1 php buffer is full, the script process will send the output data from php buffer to the system kernel for tcp to the browser for display. So, the data will be written in order: echo/print - > php buffer - > tcp buffer - > browser

php output_buffering
By default, php buffer is turned on, and the default value for buffer is 4096, or 4kb. You can find the output_buffering configuration in the ES45en.ini configuration file. When echo,print, etc output user data, the output data will be written to php output_buffering, until output_buffering is full, the data will be sent to the browser through tcp display. You can also manually activate the php output_buffering mechanism via ob_start(), so that even if the output exceeds 4kb, you don't really give the data to tcp to the browser, because ob_start() has set the php buffer space to large enough. Data is not sent to the client browser until the end of the script, or until the ob_end_flush function is called.

1. When output_buffering=4096 and output less data (less than 1 buffer)


<?php
for ($i = 0; $i < 10; $i++) {
echo $i . '<br/>';
sleep($i + 1); //
}
?>

Phenomenon: Instead of intermittent output every few seconds, the output is only seen once until the response is over, leaving browser interface 1 blank until the server script processing is over. This is because the data volume is so small that php output_buffering is not full. The order in which the data is written is echo- > php buffer- > tcp buffer- > browser

2. When output_buffering=0 and output less data (less than 1 buffer)


<?php
// through ini_set('output_buffering', 0) Do not take effect 
// Should the editor /etc/php.ini To set up output_buffering=0 disable output buffering mechanism 
//ini_set('output_buffering', 0); // Completely disable output buffering function 
for ($i = 0; $i < 10; $i++) {
echo $i . '<br/>';
flush(); // Notify the underlying operating system to give the data to the client browser as soon as possible 
sleep($i + 1); //
}
?>

Phenomenon: With the php buffering mechanism disabled, you can see intermittently the output in the browser without waiting for the script to finish executing. This is because the data does not stay in php output_buffering. The order in which the data is written is echo- > tcp buffer- > browser

3. When output_buffering=4096. And the output data is greater than 1 buffer, ob_start() is not called.


#// create 1 a 4kb Size file 
$dd if=/dev/zero of=f4096 bs=4096 count=1
<?php
for ($i = 0; $i < 10; $i++) {
echo file_get_contents('./f4096') . $i . '<br/>';
sleep($i +1);
}
?>

Phenomenon: The response is not over (http connection is not closed), intermittent output can be seen intermittently, the browser interface will not be left blank. Although the php output_buffering mechanism is enabled, the output is still intermittent rather than one-time because the output_buffering space is not enough. Each php buffering is filled, and the data is sent to the client browser.

4. When output_buffering=4096 and the output data is greater than 1 tcp buffer, call ob_start()


<?php
ob_start(); // open php buffer
for ($i = 0; $i < 10; $i++) {
echo file_get_contents('./f4096') . $i . '<br/>';
sleep($i + 1);
}
ob_end_flush();
?>

Phenomenon: You don't see the complete input until the server script is finished processing the response, and the output interval is so short that you don't feel the pause. Browser 1 keeps a blank interface waiting for server data until output. This is because once php1 calls the ob_start() function, it expands php buffer sufficiently to send the data in php buffer to the client browser until the ob_end_flush function call or the script run speed.

output buffering function

1.ob_get_level
Returns the nesting level of the output buffering mechanism to prevent templates from repeatedly nesting themselves.

1.ob_start
Activate the output_buffering mechanism. Once activated, script output is no longer directly sent to the browser, but temporarily written to the php buffer memory area.

php turns on the output_buffering mechanism by default, except that by calling the ob_start() function the output_buffering value is extended sufficiently. You can also specify $chunk_size to specify the value of output_buffering. The default value of $chunk_size is 0, which means that data from php buffer will not be sent to the browser until the script has finished running. If you set the size of $chunk_size, the data in buffer will be sent to the browser as soon as the data length in buffer reaches this value.

Of course, you can process the data in buffer by specifying $ouput_callback. For example, the ob_gzhandler function compresses the data in buffer and sends it to the browser.

2.ob_get_contents
Obtain 1 copy of data from php buffer. Note that you should call this function before the ob_end_clean() function call, otherwise ob_get_contents() returns 1 null character.

3. ob_end_flush ob_end_clean
These two functions are similar in that they both turn off the ouptu_buffering mechanism. But the difference is that ob_end_flush just flusher the data from php buffer (flush/send) to the client browser, while ob_clean_clean emptied the data from php bufeer (erase) but did not send it to the client browser. After ob_end_flush is called, the data in php buffer still exists and ob_get_contents() can still get a copy of the data in php buffer. After the call to ob_end_clean(), ob_get_contents() gets an empty string, and the browser receives no output, that is, no output.

Use case
Use of ob_start() is often seen in some template engines and page file caches. The following program code for loading templates in wet CI:


<SPAN style="WHITE-SPACE: pre">  </SPAN>/*
   * Buffer the output
   *
   * We buffer the output for two reasons:
   * 1. Speed. You get a significant speed boost.
   * 2. So that the final rendered template can be
   * post-processed by the output class.  Why do we
   * need post processing?  For one thing, in order to
   * show the elapsed page load time.  Unless we
   * can intercept the content right before it's sent to
   * the browser and then stop the timer it won't be accurate.
   */
  ob_start();
  // If the PHP installation does not support short tags we'll
  // do a little string replacement, changing the short tags
  // to standard PHP echo statements.
  if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
  {
                        // Replace short mark <?=***>
   echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  }
  else
  {
   include($_ci_path); // include() vs include_once() allows for multiple views with the same name
  }

                // Record debugging information 
  log_message('debug', 'File loaded: '.$_ci_path);
  // Return the file data if requested
  if ($_ci_return === TRUE)
  {
   $buffer = ob_get_contents();
   @ob_end_clean();
   return $buffer;
  }
  /*
   * Flush the buffer... or buff the flusher?
   *
   * In order to permit views to be nested within
   * other views, we need to flush the content back out whenever
   * we are beyond the first level of output buffering so that
   * it can be seen and included properly by the first included
   * template and any subsequent ones. Oy!
   *
   */
  if (ob_get_level() > $this->_ci_ob_level + 1)
  {
   ob_end_flush();
  }
  else
  {
                        // Adds template content to the output stream 
   $_ci_CI->output->append_output(ob_get_contents());
                        // remove buffer
   @ob_end_clean();
  }


Related articles: