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()

Comments

Comment


Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed