php ob_get_contents(); buffer

Found the dope resource when trying to store a php include inside a variable. There is a little more to it than I first thought, but not much more. Content Management Systems are built and rely upon this simpleness.

php buffer (with includes)

<?php ob_start(); // start buffer
include ('include.php');
$content = ob_get_contents(); 
// buffer contents to variable
ob_end_clean(); // end buffer and remove contents
echo $content; ?>

Because I wanted to print content using the echo function, and some of my content was in an include (include.php). Since the include function fails to parse when contained in the echo function, you need to store the include content in something echo can use (hence the variable).

No related posts.

tags: , , , ,

This entry was posted on Tuesday, July 7th, 2009 at 4:42 pm and is filed under web. You can follow any responses to this entry through the comments feed. You can leave a response, or trackback from your own site.


comment yourself