What is faster: include() or file_get_contents()?

Jack K Fouani picture Jack K Fouani · May 2, 2012 · Viewed 7.1k times · Source

I am working on a SEO system for my project and am optimizing all links with a single page.

Excerpt from .htaccess file:

RewriteRule ^(.+)$ seo.php [L,QSA]

This SEO file (seo.php) will get the requested path and parse it to be as valid url into my script.

I am using include('cat.php?catid=1') at the end of seo.php and everything is working fine, but I wonder which is faster: include() or file_get_contents()?

When I use file_get_content('cat.php?catid=1'), it displays the source of the PHP file, but when I use file_get_content('http://localhost/cat.php?catid=1'), it displays the normal page.

So, which is faster: file_get_content() or include()?

Answer

dynamic picture dynamic · May 2, 2012

They are of course different

  • Include will parse PHP code inside it
  • file_get_contents will return just the content

So if yuo want just to retrive the html content of page use file_get_contents otherwise if you need to parse PHP code use include();

Notice: if you want to retrive the content of a page hosted on your website you should use local path not web path to your resource, ie:

  • Do: file_get_contents('/home/user/site/file.html');
  • Do Not: file_get_contents('http://example.com/file.html');