What is output buffering?

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

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

7条回答
  •  无人及你
    2020-11-22 02:33

    I know that this is an old question but I wanted to write my answer for visual learners. I couldn't find any diagrams explaining output buffering on the worldwide-web so I made a diagram myself in Windows mspaint.exe.

    If output buffering is turned off, then echo will send data immediately to the Browser.

    If output buffering is turned on, then an echo will send data to the output buffer before sending it to the Browser.

    phpinfo

    To see whether Output buffering is turned on / off please refer to phpinfo at the core section. The output_buffering directive will tell you if Output buffering is on/off.

    In this case the output_buffering value is 4096 which means that the buffer size is 4 KB. It also means that Output buffering is turned on, on the Web server.

    php.ini

    It's possible to turn on/off and change buffer size by changing the value of the output_buffering directive. Just find it in php.ini, change it to the setting of your choice, and restart the Web server. You can find a sample of my php.ini below.

    ; Output buffering is a mechanism for controlling how much output data
    ; (excluding headers and cookies) PHP should keep internally before pushing that
    ; data to the client. If your application's output exceeds this setting, PHP
    ; will send that data in chunks of roughly the size you specify.
    ; Turning on this setting and managing its maximum buffer size can yield some
    ; interesting side-effects depending on your application and web server.
    ; You may be able to send headers and cookies after you've already sent output
    ; through print or echo. You also may see performance benefits if your server is
    ; emitting less packets due to buffered output versus PHP streaming the output
    ; as it gets it. On production servers, 4096 bytes is a good setting for performance
    ; reasons.
    ; Note: Output buffering can also be controlled via Output Buffering Control
    ;   functions.
    ; Possible Values:
    ;   On = Enabled and buffer is unlimited. (Use with caution)
    ;   Off = Disabled
    ;   Integer = Enables the buffer and sets its maximum size in bytes.
    ; Note: This directive is hardcoded to Off for the CLI SAPI
    ; Default Value: Off
    ; Development Value: 4096
    ; Production Value: 4096
    ; http://php.net/output-buffering
    output_buffering = 4096
    

    The directive output_buffering is not the only configurable directive regarding Output buffering. You can find other configurable Output buffering directives here: http://php.net/manual/en/outcontrol.configuration.php

    Example: ob_get_clean()

    Below you can see how to capture an echo and manipulate it before sending it to the browser.

    // Turn on output buffering  
    ob_start();  
    
    echo 'Hello World';  // save to output buffer
    
    $output = ob_get_clean();  // Get content from the output buffer, and discard the output buffer ...
    $output = strtoupper($output); // manipulate the output  
    
    echo $output;  // send to output stream / Browser
    
    // OUTPUT:  
    HELLO WORLD
    

    Examples: Hackingwithphp.com

    More info about Output buffer with examples can be found here:

    http://www.hackingwithphp.com/13/0/0/output-buffering

提交回复
热议问题