Denis Denis - 9 months ago 75
PHP Question

Output Buffer versus file_get_contents in PHP

What are the differences between these two ways of obtaining file contents? Which one is better and more efficient? I think they both obtain the same results but I really don't know which method is better.

For example.

This code uses output buffering to obtain the file's content:

$output .= ob_get_contents();

This code uses file_get_contents and gets the same results.

$output = file_get_contents('foo/bar.tpl');


Well, the second example will just output the file content as raw text, while in the first the file content will be parsed by the PHP interpreter, meaning if some PHP code exists within it, it will be executed!