PHP
  Home arrow PHP arrow Page 4 - Output Buffering
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Output Buffering
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-09-02


    Table of Contents:
  • Output Buffering
  • Login Script
  • Code Explained
  • Further suggestions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Output Buffering - Further suggestions
    ( Page 4 of 4 )

    You can set the maximum buffer size in your PHP configuration file. The default is 4096 characters. Below is an extract from a PHP configuration file, showing the default setting:


    ; - log_errors = On [Security]

    ; This directive complements the above one. Any errors that occur during the

    ; execution of your script will be logged (typically, to your server's error log,

    ; but can be configured in several ways). Along with setting display_errors to off,

    ; this setup gives you the ability to fully understand what may have gone wrong,

    ; without exposing any sensitive information to remote users.

    ; - output_buffering = 4096 [Performance]

    ; Set a 4KB output buffer. Enabling output buffering typically results in less

    ; writes, and sometimes less packets sent on the wire, which can often lead to

    ; better performance. The gain this directive actually yields greatly depends

    ; on which Web server you're working with, and what kind of scripts you're using.




    You can send compressed output to browsers by starting output control with ob_start('ob_gzhandler’), minimizing the download size of your pages. The ob_gzhandler() function determines the appropriate type of compression to use (gzip,deflate, or none) based on what the browser can accept. Compression is beneficial only on larger web pages, and in order to use it, the output_buffering setting in the php.ini file must be set to off .

    The ob_get_length() function returns the length of the current buffer contents.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Untitled Document</title>

    </head>


    <body>


    <?php


    ob_start();


    echo "Go for it ";


    $oblength1 = ob_get_length();


    echo "World";


    $oblength2= ob_get_length();


    ob_end_clean();


    echo $oblength1 . ", ." . $oblength2;

    var_dump($oblength1, $oblength2);

    ?>


    </body>

    </html>


    The above code outputs the following:

    10, .15

    int(10)

    int(15)

    The ob_get_contents() function returns the current buffer so that it can be assigned to a variable if needed:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Untitled Document</title>

    </head>


    <body>


    <?php


    ob_start();


    echo "My name is David Web ";


    $obcontents1 = ob_get_contents();


    echo "and i'm very tired right now";


    $obcontents2 = ob_get_contents();


    ob_end_clean();


    var_dump($obcontents1, $obcontents2);

    ?>


    </body>

    </html>


    The code above outputs:

    string(21) "My name is David Web "

    string(49) "My name is David Web and i'm very tired right now"

    PHP automatically runs the ob_end_flush() function at the end of a script if it is not otherwise called. But it is better to get into the habit of calling it yourself.



     
     
    >>> More PHP Articles          >>> More By David Web
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek