PHP 中的输出缓冲区与 file_get_contents

3 php templates output-buffering file-get-contents

这两种获取文件内容的方式有什么区别?哪一种更好,更有效?我认为他们都获得了相同的结果,但我真的不知道哪种方法更好。

例如。

此代码使用输出缓冲来获取文件的内容:

ob_start();
include('foo/bar.tpl');
$output .= ob_get_contents();
ob_end_clean();
Run Code Online (Sandbox Code Playgroud)

此代码使用 file_get_contents 并获得相同的结果。

$output = file_get_contents('foo/bar.tpl');
Run Code Online (Sandbox Code Playgroud)

php*_*dev 6

好吧,第二个示例将仅将文件内容输出为原始文本,而在第一个示例中,文件内容将由 解析PHP interpreter,这意味着如果其中PHP存在某些代码,它将被执行!