What is output buffering?

后端 未结 7 2120
不思量自难忘°
不思量自难忘° 2020-11-22 01:49

What is output buffering and why is one using it in PHP?

相关标签:
7条回答
  • 2020-11-22 02:42

    Output buffering is used by PHP to improve performance and to perform a few tricks.

    • You can have PHP store all output into a buffer and output all of it at once improving network performance.

    • You can access the buffer content without sending it back to browser in certain situations.

    Consider this example:

    <?php
        ob_start( );
        phpinfo( );
        $output = ob_get_clean( );
    ?>
    

    The above example captures the output into a variable instead of sending it to the browser. output_buffering is turned off by default.

    • You can use output buffering in situations when you want to modify headers after sending content.

    Consider this example:

    <?php
        ob_start( );
        echo "Hello World";
        if ( $some_error )
        {
            header( "Location: error.php" );
            exit( 0 );
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题