什么是更快:include()或file_get_contents()?

Jac*_*ani 7 php include file-get-contents

我正在为我的项目开发一个SEO系统,并用一个页面优化所有链接.

.htaccess文件摘录:

RewriteRule ^(.+)$ seo.php [L,QSA]
Run Code Online (Sandbox Code Playgroud)

这个SEO文件(seo.php)将获取请求的路径并将其解析为我的脚本中的有效URL.

include('cat.php?catid=1')在最后使用,seo.php一切都很好,但我想知道哪个更快:include()或者file_get_contents()

当我使用file_get_content('cat.php?catid=1')它时,它显示PHP文件的来源,但是当我使用file_get_content('http://localhost/cat.php?catid=1')它时,它显示正常页面.

那么,哪个更快:file_get_content()或者include()

dyn*_*mic 13

它们当然是不同的

  • Include将在其中解析PHP代码
  • file_get_contents将仅返回内容

所以如果你只想要检索页面的html内容,file_get_contents否则如果你需要解析PHP代码使用include();

注意:如果要检索网站上托管的页面内容,则应使用本地路径而不是资源的Web路径,即:

  • 做: file_get_contents('/home/user/site/file.html');
  • 不要: file_get_contents('http://example.com/file.html');