Output Buffer versus file_get_contents in PHP
Asked Answered
H

1

5

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:

ob_start();
include('foo/bar.tpl');
$output .= ob_get_contents();
ob_end_clean();

This code uses file_get_contents and gets the same results.

$output = file_get_contents('foo/bar.tpl');
Homeric answered 7/10, 2014 at 20:17 Comment(0)
H
9

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!

Hoekstra answered 7/10, 2014 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.