PHP OUTPUT BUFFERING



 PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanch...
“The Output Control functions allow you to control when output
    is sent from the script. This can be useful in several ...
How often has this
                                      happened to you?




PHP Output Buffering :: David Ross :: Suburb...
Warning: Cannot modify header information - headers already sent
   by (output started at /path/to/script.php:123) in /pat...
What about this?




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


PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Dev...
Content might not float
               correctly if it isn’t all there yet.
           (This happened a lot more on dial-up...
Output buffering fixes
                       both of these common issues.




PHP Output Buffering :: David Ross :: Suburb...
ENABLE OUTPUT BUFFERING
                                              (PHP.INI VERSION)


• output_buffering              ...
Buffers output.
                                   Fixes both problems.




PHP Output Buffering :: David Ross :: Suburban...
But there has to be more.

                        Or else this would be a lame
                                presentati...
ENABLE OUTPUT BUFFERING
                                                 (CODE VERSION)


• ob_start(); //         Start a...
ob_start

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

•   Optional $output...
Call ob_start() at the top of your script
       same as output_buffering = On in php.ini




PHP Output Buffering :: Davi...
CACHING

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

• If   not, continue with the script

• Use o...
MORE INFO ON CACHING
http://www.slideshare.net/csixty4/caching-data-for-performance




         PHP Output Buffering :: D...
MODIFY THE BUFFER


• ob_get_contents()                        or $output_callback again

• Get   the buffer as a string

...
MODIFY THE BUFFER
                          (WHAT CAN YOU DO WITH IT?)




• Templating       system

• Form   building li...
(At this point, Mike Creuzer spent a few
      minutes talking about a script he wrote
        that calls a Twitter librar...
NESTING BUFFERS



• Call   ob_start() as many times as you want

• Useful   for capturing output from libraries as string...
NESTING BUFFERS

ob_start();


echo “stuff”;


ob_start(); // create a new buffer


generateATonOfHTML();


$buff = ob_get...
CAVEAT
              You knew there had to be a catch




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




PHP Output Buffering :: David Ro...
BUFFERS TAKE UP MEMORY

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

• Ifset to...
Your script could run out of memory.




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meet...
Your server could run out of RAM and have to swap
               (sloooooooooooow)




  PHP Output Buffering :: David Ros...
(Fairly) easy to mitigate:
                                    buy RAM
                          and increase memory_limit...
Questions?




PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.o...
Upcoming SlideShare
Loading in …5
×

PHP Output Buffering

5,247 views

Published on

Published in: Technology
  • Be the first to comment

PHP Output Buffering

  1. 1. PHP OUTPUT BUFFERING PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  2. 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. 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. 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. 5. What about this? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  6. 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. 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. 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. 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. 10. Buffers output. Fixes both problems. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
  11. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 28. Questions? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009

×