Your SlideShare is downloading. ×
PHP Output Buffering
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

PHP Output Buffering

4,586
views

Published on

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,586
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
51
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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