Home  >  Article  >  Backend Development  >  How to enable output buffering using ob_start() function in PHP? (code example)

How to enable output buffering using ob_start() function in PHP? (code example)

青灯夜游
青灯夜游Original
2019-03-09 11:05:503524browse

In PHP, we can use the ob_start() function to enable output buffering. The following article will give you a brief understanding of how to use the ob_start() function. I hope it will be helpful to you. [Video tutorial recommendation: PHP tutorial]

How to enable output buffering using ob_start() function in PHP? (code example)

PHP is an interpreted language, and each statement is executed one after another, so PHP Tends to send HTML to the browser in chunks, thus reducing performance. Using output buffering, all output information is no longer sent directly to the browser, but is saved in the output buffer or string variable, and the contents of the buffer will not be sent to the browser until the last statement in the PHP script is executed. The browser performs rendering and display.

But output buffering is not enabled by default; in order to enable output buffering, the ob_start() function must be used before sending back any HTML content in the script.

Basic syntax:

ob_start ()

Parameters: The ob_start() function can accept a bunch of optional parameters, as shown below:

●Callback function: This is an optional parameter that requires a function that obtains the contents of the output buffer and returns a string, which will be sent to the browser for rendering. Callback functions are often used to compress HTML content.

● Chunk size: This is another optional parameter that sets the output buffer size of the provided size and output immediately when the buffer is full or exceeded.

● Flags: This is another optional parameter that accepts a bitmask to control the operations that can be performed on the output buffer. Pass this parameter to restrict access. Default permissions allow access to clean, flush and delete buffers.

Return type: ob_start() function returns TRUE when successful, otherwise it returns FALSE.

Code example:

<?php 
header("content-type:text/html;charset=utf-8");
function callback($buffer){
//返回缓冲区内的所有东西
    return (strtoupper($buffer.",javascript,html,css!")); 
} 
  
ob_start("callback"); 
echo "Hello php"; 
ob_end_flush(); 
  
?>

Output:

How to enable output buffering using ob_start() function in PHP? (code example)

Description:

●Want To use the buffer, you need to start the buffer first.

●The output buffer flag can be of four types:

1. PHP_OUTPUT_HANDLER_CLEANABLE (only clean)

2. PHP_OUTPUT_HANDLER_FLUSHABLE (only flush)

3. PHP_OUTPUT_HANDLER_REMOVABLE(only remove)

 4. PHP_OUTPUT_HANDLER_STDFLAGS(allowed every operation).

●Output buffers are stackable, so nested ob_start is allowed if closed/flushed sequentially () method and works as needed.

●ob_end_flush() function is used to close the output buffer after ending (sending) the contents of the output buffer.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to enable output buffering using ob_start() function in PHP? (code example). 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