Reputation: 7
I actually ran into a problem today. I have got a site where multiple pages have the same layout. Today I had to change a thing on this layout and had to change every single page. This made me think about solutions for this.
So, my question: Would the performance and loading speed of the website be affected if I had the layout HTML in one single file and read it using PHP on every page? This would make it a lot easier for me, since I only had to change the layout in that single file. Also, how should I read this file. At the moment, I would use the following code:
for ($count=0; $count < $dirAmount; $count++) {
$path = " //path\\ ";
$fileText = split("--", file_get_contents($path));
echo " //code\\ "
unset($fileText);
}
But would there be a faster way?
BTW, if this is too trivial, please note. I couldn't find much other information that actually was clear enough to help.
Upvotes: 0
Views: 57
Reputation: 72857
Simply use php's include
or require
to include the file.
It will affect performance a little bit, but that tiny little performance cost is insignificant compared to the ease of having one file you can use in multiple places.
One suggestion would be to use full file paths on include/require statements. Normalizing a relative file path can be expensive; giving PHP the absolute path (or even “./file.inc”) avoids the extra step. - Source
Upvotes: 1
Reputation: 12356
I would suggest you consider using smarty or twig if you want to start using templates for your website. Those engines optimize page loads by pre-caching pages and optimizing them to load as quickly as possible.
As for your original question, no, especially if the file was called repeatedly by the entire website, it would most likely get cached into RAM and load pretty fast since it was being called often. As Cerbus mentioned, using include or require would probably be the fastest way to process the files.
Upvotes: 1