Php Return Contents of an Include as String

Last modified: 
Tuesday, May 5th, 2015
Topics: 
PHP

Overview

Sometimes you may want to get the contents of a file after it has been parsed by the PHP interpreter. This function uses output buffering to capture the contents of an included file and returns the result as a string. The function definition is taken verbatim from the PHP include manual page.

/**
 * Returns the contents of an include as a string.
 */
function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    }
    return false;
}

References

PHP: include - Manual | http://www.php.net/manual/en/function.include.php


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.