Capture the Output of an Included File

It is possible to capture the output of an include to a variable. This is useful for caching and code generation. I found this feature when I was creating a code generation script for IFrame.

$string = getIncludeContents('template.php');

function getIncludeContents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}

More on include()

blog comments powered by Disqus
Subscribe to Feed