输出缓冲区与PHP中的file_get_contents

时间:2014-10-07 20:17:04

标签: php templates file-get-contents output-buffering

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

例如。

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

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

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

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

1 个答案:

答案 0 :(得分:4)

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