php - 输出缓冲区与 PHP 中的 file_get_contents

标签 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');

最佳答案

好吧,第二个例子只是将文件内容输出为原始文本,而在第一个例子中,文件内容将被PHP解释器解析,这意味着如果一些PHP 代码存在于其中,它将被执行!

关于php - 输出缓冲区与 PHP 中的 file_get_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26244498/

相关文章:

java - 一起使用 Solr 和 Zends Lucene 端口

c++ - 类模板中的友元函数和错误 LNK2019

templates - 如何在 EmberJS 中呈现除 application.hbs 以外的模板?

c++ - 编译错误 : unresolved overloaded function type

javascript - 在 jquery.dataTable 中同时输入两个字段

php - 从单个帖子获取 Wordpress 类别

windows - Get-Content 的 PowerShell 问题

php - 将文件加载为包含变量定义的字符串

php - 来自远程客户端和 SSL 的 file_get_contents

php - Composer 忽略 zip 依赖项的 composer.json 文件