SlideShare a Scribd company logo
PHP OUTPUT BUFFERING



 PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
“The Output Control functions allow you to control when output
    is sent from the script. This can be useful in several different
 situations, especially if you need to send headers to the browser
 after your script has began outputting data. The Output Control
functions do not affect headers sent using header() or setcookie(),
  only functions such as echo() and data between blocks of PHP
                                 code.”

   Source: http://www.php.net/manual/en/intro.outcontrol.php




         PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
How often has this
                                      happened to you?




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
Warning: Cannot modify header information - headers already sent
   by (output started at /path/to/script.php:123) in /path/to/
                  otherscript.php on line 321




         PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
What about this?




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
(Don’t worry, it’s fake. Yahoo!, please don’t sue.)


PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
Content might not float
               correctly if it isn’t all there yet.
           (This happened a lot more on dial-up)




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
Output buffering fixes
                       both of these common issues.




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
ENABLE OUTPUT BUFFERING
                                              (PHP.INI VERSION)


• output_buffering                = On

• output_buffering                = 4096 (bytes)




         PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
Buffers output.
                                   Fixes both problems.




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
But there has to be more.

                        Or else this would be a lame
                                presentation.




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
ENABLE OUTPUT BUFFERING
                                                 (CODE VERSION)


• ob_start(); //         Start a new output buffer

• ob_flush(); //          Send contents to browser

• ob_end_flush(); //                    Send contents to browser, destroy buffer

• ob_clean(); //          Discard buffer contents

• ob_end_clean(); //                     Discard buffer contents & destroy buffer

• ob_get_contents(); //                          Contents of the current buffer
           PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
ob_start

•   bool ob_start ([ callback $output_callback [, int $chunk_size [,
    bool $erase ]]] )

•   Optional $output_callback routine called before outputting or
    discarding buffer. Gets copy of buffer (string) and must return
    string.

•   Optional $chunk_size (size of the buffer)

•   Optional $erase (false == don’t destroy buffer until script ends)

             PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
Call ob_start() at the top of your script
       same as output_buffering = On in php.ini




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
CACHING

• At    script’s start, see if there’s a cached copy of the output

• If   not, continue with the script

• Use ob_get_contents() or $output_callback to get buffer
  contents and cache it

• Next     time script runs, it’ll find the cached copy


             PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
MORE INFO ON CACHING
http://www.slideshare.net/csixty4/caching-data-for-performance




         PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
MODIFY THE BUFFER


• ob_get_contents()                        or $output_callback again

• Get   the buffer as a string

• Do    str_replace, maybe parse it as XML

• Return   a string version


            PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
MODIFY THE BUFFER
                          (WHAT CAN YOU DO WITH IT?)




• Templating       system

• Form   building library

• Unicode    character replacement (accents, fancy quotes)



           PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
(At this point, Mike Creuzer spent a few
      minutes talking about a script he wrote
        that calls a Twitter library and uses
      an output buffer to capture the results,
   then pulls what he wants out of the output.)




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
NESTING BUFFERS



• Call   ob_start() as many times as you want

• Useful   for capturing output from libraries as strings




            PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
NESTING BUFFERS

ob_start();


echo “stuff”;


ob_start(); // create a new buffer


generateATonOfHTML();


$buff = ob_get_contents(); ob_end_clean();


$buff = str_replace(‘Dog’, ‘Cat’, $buff);


echo $buff;




                  PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
CAVEAT
              You knew there had to be a catch




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
If output buffering is so good,
                   why isn’t it turned on by default?




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
BUFFERS TAKE UP MEMORY

• If
   set to “On” or ob_start() called w/o $chunk_size, uses as
  much RAM as needed

• Ifset to a number or ob_start() called with $chunk_size, uses
  that much memory

• Memory     use multiplied by # of nested buffers

• Each   simultaneous script needs RAM

           PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
Your script could run out of memory.




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
Your server could run out of RAM and have to swap
               (sloooooooooooow)




  PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
(Fairly) easy to mitigate:
                                    buy RAM
                          and increase memory_limit
                                     in php.ini




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
Questions?




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009

More Related Content

What's hot (19)

KEY
Php Power Tools
Michelangelo van Dam
 
PPTX
PHP 5.6 New and Deprecated Features
Mark Niebergall
 
PPS
PHP - History, Introduction, Summary, Extensions and Frameworks
Royston Olivera
 
KEY
Site Performance - From Pinto to Ferrari
Joseph Scott
 
PDF
Internationalization with TYPO3
digiparden GmbH
 
PDF
An Introduction to Symfony
xopn
 
PPTX
Php7 hhvm and co
Pierre Joye
 
PPTX
10 Most Important Features of New PHP 5.6
Webline Infosoft P Ltd
 
PPT
Build Automation of PHP Applications
Pavan Kumar N
 
KEY
LAMP Optimization
Dave Ross
 
ODP
30 Minutes To CPAN
daoswald
 
KEY
Ant vs Phing
Manuel Baldassarri
 
PDF
Php Inside - confoo 2011 - Derick Rethans
Bachkoutou Toutou
 
PPT
Php intro
Jennie Gajjar
 
PPTX
From PHP to Hack as a Stack and Back
Timothy Chandler
 
PDF
PHP Files: An Introduction
Jacques Woodcock
 
ODP
Phing - A PHP Build Tool (An Introduction)
Michiel Rook
 
PDF
Talking to Web Services
DrupalcampAtlanta2012
 
PDF
Phing: Building with PHP
hozn
 
Php Power Tools
Michelangelo van Dam
 
PHP 5.6 New and Deprecated Features
Mark Niebergall
 
PHP - History, Introduction, Summary, Extensions and Frameworks
Royston Olivera
 
Site Performance - From Pinto to Ferrari
Joseph Scott
 
Internationalization with TYPO3
digiparden GmbH
 
An Introduction to Symfony
xopn
 
Php7 hhvm and co
Pierre Joye
 
10 Most Important Features of New PHP 5.6
Webline Infosoft P Ltd
 
Build Automation of PHP Applications
Pavan Kumar N
 
LAMP Optimization
Dave Ross
 
30 Minutes To CPAN
daoswald
 
Ant vs Phing
Manuel Baldassarri
 
Php Inside - confoo 2011 - Derick Rethans
Bachkoutou Toutou
 
Php intro
Jennie Gajjar
 
From PHP to Hack as a Stack and Back
Timothy Chandler
 
PHP Files: An Introduction
Jacques Woodcock
 
Phing - A PHP Build Tool (An Introduction)
Michiel Rook
 
Talking to Web Services
DrupalcampAtlanta2012
 
Phing: Building with PHP
hozn
 

Similar to PHP Output Buffering (20)

PDF
Tips
mclee
 
PPTX
A brief to PHP 7.3
Xinchen Hui
 
PPTX
PHP 5.3
Chris Stone
 
PDF
Dutch php conference_2010_opm
isnull
 
PDF
PHP Unit-1 Introduction to PHP
Lariya Minhaz
 
PDF
Php 5.6 From the Inside Out
Ferenc Kovács
 
PDF
Everything new with PHP 7.3
Damien Seguy
 
PPTX
Taming the resource tiger
Elizabeth Smith
 
ODP
PHP BASIC PRESENTATION
krutitrivedi
 
PPTX
LAMP TECHNOLOGY BY SAIKIRAN PANJALA
Saikiran Panjala
 
PDF
An introduction to PHP 5.4
Giovanni Derks
 
PDF
What's new in PHP 7.4
Andrea Maccis
 
ODP
The promise of asynchronous php
Wim Godden
 
PPTX
object oriented programming in PHP & Functions
BackiyalakshmiVenkat
 
PDF
2013 - Dustin whittle - Escalando PHP en la vida real
PHP Conference Argentina
 
PPTX
Northeast PHP - High Performance PHP
Jonathan Klein
 
PPTX
Taming the resource tiger
Elizabeth Smith
 
PDF
PHP Streams
G Woo
 
PDF
Continuous Integration In Php
Wilco Jansen
 
Tips
mclee
 
A brief to PHP 7.3
Xinchen Hui
 
PHP 5.3
Chris Stone
 
Dutch php conference_2010_opm
isnull
 
PHP Unit-1 Introduction to PHP
Lariya Minhaz
 
Php 5.6 From the Inside Out
Ferenc Kovács
 
Everything new with PHP 7.3
Damien Seguy
 
Taming the resource tiger
Elizabeth Smith
 
PHP BASIC PRESENTATION
krutitrivedi
 
LAMP TECHNOLOGY BY SAIKIRAN PANJALA
Saikiran Panjala
 
An introduction to PHP 5.4
Giovanni Derks
 
What's new in PHP 7.4
Andrea Maccis
 
The promise of asynchronous php
Wim Godden
 
object oriented programming in PHP & Functions
BackiyalakshmiVenkat
 
2013 - Dustin whittle - Escalando PHP en la vida real
PHP Conference Argentina
 
Northeast PHP - High Performance PHP
Jonathan Klein
 
Taming the resource tiger
Elizabeth Smith
 
PHP Streams
G Woo
 
Continuous Integration In Php
Wilco Jansen
 
Ad

More from Dave Ross (20)

KEY
Stylesheets of the future with Sass and Compass
Dave Ross
 
KEY
HTML5 History & Features
Dave Ross
 
PPT
A geek's guide to getting hired
Dave Ross
 
KEY
NoSQL & MongoDB
Dave Ross
 
PDF
Date and Time programming in PHP & Javascript
Dave Ross
 
KEY
Simulated Eye Tracking with Attention Wizard
Dave Ross
 
KEY
What's new in HTML5?
Dave Ross
 
KEY
The Canvas Tag
Dave Ross
 
KEY
Wordpress
Dave Ross
 
PPT
Lamp Stack Optimization
Dave Ross
 
PPT
The FPDF Library
Dave Ross
 
PPT
FirePHP
Dave Ross
 
PPT
Bayesian Inference using b8
Dave Ross
 
PPT
SQL Injection in PHP
Dave Ross
 
KEY
Web App Security: XSS and CSRF
Dave Ross
 
KEY
The Mobile Web: A developer's perspective
Dave Ross
 
KEY
Balsamiq Mockups
Dave Ross
 
KEY
Lint - PHP & Javascript Code Checking
Dave Ross
 
KEY
Cufon - Javascript Font Replacement
Dave Ross
 
KEY
Firebug
Dave Ross
 
Stylesheets of the future with Sass and Compass
Dave Ross
 
HTML5 History & Features
Dave Ross
 
A geek's guide to getting hired
Dave Ross
 
NoSQL & MongoDB
Dave Ross
 
Date and Time programming in PHP & Javascript
Dave Ross
 
Simulated Eye Tracking with Attention Wizard
Dave Ross
 
What's new in HTML5?
Dave Ross
 
The Canvas Tag
Dave Ross
 
Wordpress
Dave Ross
 
Lamp Stack Optimization
Dave Ross
 
The FPDF Library
Dave Ross
 
FirePHP
Dave Ross
 
Bayesian Inference using b8
Dave Ross
 
SQL Injection in PHP
Dave Ross
 
Web App Security: XSS and CSRF
Dave Ross
 
The Mobile Web: A developer's perspective
Dave Ross
 
Balsamiq Mockups
Dave Ross
 
Lint - PHP & Javascript Code Checking
Dave Ross
 
Cufon - Javascript Font Replacement
Dave Ross
 
Firebug
Dave Ross
 
Ad

Recently uploaded (20)

PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PDF
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
Open Source Milvus Vector Database v 2.6
Zilliz
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
Practical Applications of AI in Local Government
OnBoard
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
The Growing Value and Application of FME & GenAI
Safe Software
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Open Source Milvus Vector Database v 2.6
Zilliz
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 

PHP Output Buffering

  • 1. PHP OUTPUT BUFFERING PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 2. “The Output Control functions allow you to control when output is sent from the script. This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data. The Output Control functions do not affect headers sent using header() or setcookie(), only functions such as echo() and data between blocks of PHP code.” Source: http://www.php.net/manual/en/intro.outcontrol.php PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 3. How often has this happened to you? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 4. Warning: Cannot modify header information - headers already sent by (output started at /path/to/script.php:123) in /path/to/ otherscript.php on line 321 PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 5. What about this? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 6. (Don’t worry, it’s fake. Yahoo!, please don’t sue.) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 7. Content might not float correctly if it isn’t all there yet. (This happened a lot more on dial-up) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 8. Output buffering fixes both of these common issues. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 9. ENABLE OUTPUT BUFFERING (PHP.INI VERSION) • output_buffering = On • output_buffering = 4096 (bytes) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 10. Buffers output. Fixes both problems. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 11. But there has to be more. Or else this would be a lame presentation. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 12. ENABLE OUTPUT BUFFERING (CODE VERSION) • ob_start(); // Start a new output buffer • ob_flush(); // Send contents to browser • ob_end_flush(); // Send contents to browser, destroy buffer • ob_clean(); // Discard buffer contents • ob_end_clean(); // Discard buffer contents & destroy buffer • ob_get_contents(); // Contents of the current buffer PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 13. ob_start • bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] ) • Optional $output_callback routine called before outputting or discarding buffer. Gets copy of buffer (string) and must return string. • Optional $chunk_size (size of the buffer) • Optional $erase (false == don’t destroy buffer until script ends) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 14. Call ob_start() at the top of your script same as output_buffering = On in php.ini PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 15. CACHING • At script’s start, see if there’s a cached copy of the output • If not, continue with the script • Use ob_get_contents() or $output_callback to get buffer contents and cache it • Next time script runs, it’ll find the cached copy PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 16. MORE INFO ON CACHING http://www.slideshare.net/csixty4/caching-data-for-performance PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 17. MODIFY THE BUFFER • ob_get_contents() or $output_callback again • Get the buffer as a string • Do str_replace, maybe parse it as XML • Return a string version PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 18. MODIFY THE BUFFER (WHAT CAN YOU DO WITH IT?) • Templating system • Form building library • Unicode character replacement (accents, fancy quotes) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 19. (At this point, Mike Creuzer spent a few minutes talking about a script he wrote that calls a Twitter library and uses an output buffer to capture the results, then pulls what he wants out of the output.) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 20. NESTING BUFFERS • Call ob_start() as many times as you want • Useful for capturing output from libraries as strings PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 21. NESTING BUFFERS ob_start(); echo “stuff”; ob_start(); // create a new buffer generateATonOfHTML(); $buff = ob_get_contents(); ob_end_clean(); $buff = str_replace(‘Dog’, ‘Cat’, $buff); echo $buff; PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 22. CAVEAT You knew there had to be a catch PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 23. If output buffering is so good, why isn’t it turned on by default? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 24. BUFFERS TAKE UP MEMORY • If set to “On” or ob_start() called w/o $chunk_size, uses as much RAM as needed • Ifset to a number or ob_start() called with $chunk_size, uses that much memory • Memory use multiplied by # of nested buffers • Each simultaneous script needs RAM PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 25. Your script could run out of memory. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 26. Your server could run out of RAM and have to swap (sloooooooooooow) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 27. (Fairly) easy to mitigate: buy RAM and increase memory_limit in php.ini PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  • 28. Questions? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009