Penggunaan fungsi BUFFERING. pada PHP

The manual is a little shy on explaining that output buffers are nested, and that "turns off output buffering" means turning off the highest nested buffer.  See ob_get_level (for a useful function, but still no explanation)

<?php
    ob_start
();
    echo
"1:blah\n";
   
ob_start();
    echo
"2:blah";
   
// ob_get_clean() returns the contents of the last buffer opened.  The first "blah" and the output of var_dump are flushed from the top buffer on exit
   
var_dump(ob_get_clean());
    exit;
?>

puts:
    1:blah
    string(6) "2:blah"

Prior to realising this, I had thought PHP's ob functionality left more to be desired.  I *really* wish I knew earlier.

Follow the below steps to enable and disable PHP output buffering on your Linux server.

PHP output_buffering is used to keep some data in memory instead of sending it to web browser.

Output is stored in memory for some time and it is sent to the web browser when the script execution is completed.

How to Enable PHP output_buffering

1. Log into your Linux server as ‘root’ user or login as any user with sudo privileges

2. Find the path of php.ini by running the command ‘php --ini’

Find and edit php.ini

  3. Open php.ini using vi editor and change ‘output_buffering = On’ in php.ini file

How to Enable PHP output_buffering

  4. Save the php.ini file and exit

5. Restart the web server
 

[[email protected] /]# systemctl restart httpd

6. Create phpinfo page to verify output_buffering is enabled

phpinfo – output_buffering enabled

  ‘output_buffering = 1’ means output_buffering is enabled and buffer is unlimited (Use with caution)

You can set custom value for output_buffering. Example : ‘output_buffering = 4096’

How to set custom value for output_buffering


 

How to Disable PHP output_buffering

1. Log into you Linux server as ‘root’ user

2. Run the command ‘php --ini’ to find the loaded configuration file
 

[[email protected] /]# php –ini | grep Loaded
Loaded Configuration File: /etc/php.ini

 
3. Change the line to ‘output_buffering = Off’ in php.ini file
 

[[email protected] /]# vi /etc/php.ini

Change the line ‘output_buffering = On’ to ‘output_buffering = Off’

 
4. Save the php.ini file and exit

5. Restart the webserver for changes to take effect
 

[[email protected] /]# systemctl restart httpd
[[email protected] /]# systemctl status httpd

 
6. Create phpinfo file to verify output_buffering is disabled

‘output_buffering = 0’ means output_buffering is disabled.

How to check whether PHP output_buffering is enabled or disabled

1. Log into your Linux server as ‘root’ or sudo user

2. Run the command ‘php --ini’ to find the PHP configuration file

  3. Grep ‘output_buffering’ in PHP configuration file

[[email protected] /]# grep output_buffering /etc/php.ini
; output_buffering
output_buffering = 4096
; performance, enable output_buffering in addition.

4. You can also check output_buffering by creating phpinfo page in site document root

output_buffering is enabled on the above linux server.

Fungsi PHP Output Buffering Control
a. flush( )
    flush digunakan untuk mrngosongkan buffer memory yang digunakan sebagai

   

penyimpan hasil output.

Aturan Penyimpanan:

        void flush (void) ;

      Contoh:

        <?php
                 ob_start ( ) ;
                        for ($i = 0;$i<10;$i++) { 
                 printf ($i) ;
                  ob flush ( ) ;
                  usleep (500000) ;
                  }
         ?>

 b. ob_flush( )
     ob_flush digunakan untuk mengosongkan output buffer.

Aturan Penulisan:

         void flush (void) ;

Contoh:

         <?php
                  ob_start ( ) ;
                  for ($i = 0;$i<10;$i++) { 
                       printf ($i) ;
                        ob flush ( ) ;
                        usleep (500000) ;
                  }
          ?>

 c. ob_get_contents( )
     ob_get_contents digunakan untuk mengambil konten pada output buffer.
      

Aturan Penyimpanan:

         string ob_get_contents (void) ;

Contoh:

         <?php
                  ob_start( ) ;
                  echo "Output Buffer" . "<br />" ;
                  $buff = ob_get_contents( ) ;
                  echo $buff ;
          ?>

 d. ob_start( )
     ob_start digunakan untuk mengaktifkan output buffer, memory yang digunakan
     sebagai penyimpanan sementara hasil output.
      

Aturan Penulisan:

         bool ob_start ([ callbackc $output_callback [, int $chunk_size [, bool $erase ]]]) ;

Contoh:

         <?php
                  function tulis ($buff) { 
                 return (str_replace ("php-mysql","PHP MYSQL",$buff)) ; } 
                 ob_start ("tulis") ;
          ?>
           <p>php-mysql</p>
           >?php ob_end_flush( ) ;     ?>

 e. ob_clean( )
     ob_clean digunakan untuk menghapus output buffer.
      

Aturan Penulisan:

         void ob_and_clean (void) ;

Contoh:

         <?php
                  function tulis ($buff) { 
                         return (str_replace ("php-mysql","PHP MYSQL",$buff)) ; }
                  ob_start ("tulis") ;
           ?>
           <p>php-mysql</p>
           >?php ob_end_flush( ) ;     ?> 

f. ob_end_clean( )
     ob_end_clean digunakan untuk menghapus output buffer dan menutup output buffer.
      

Aturan Penulisan:

         bool ob_end_clean (void) ;

Contoh:

         <?php
                  function tulis ($buff) { 
                          return (str_replace ("php-mysql","PHP MYSQL",$buff)) ; }
                  ob_start ("tulis") ;
          ?>
          <p>php-mysql</p>
           >?php ob_end_clean( ) ;       ?> 

g. ob_end_flush( )
     ob_end_flush digunakan untuk mengosongkan output buffer dan menutup output buffer.
      

Aturan Penulisan:

         bool ob_end_flush(viod) ;

Contoh:

         <?php
                  function tulis ($buff) { 
                          return (str_replace ("php-mysql","PHP MYSQL",$buff)) ; }
                  ob_start ("tulis") ;
          ?>
           <p>php-mysql</p>
            >?php ob_end_flush( ) ;     ?>