Including content to your index.php or writing code directly to index file? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Including content to your index.php or writing code directly to index file?

Hey! I see sometimes that people include their header, content, footer etc with php to index.php file (example: include("header.php")). Why is it better to include these things than write all the code to the index.php file? Can someone give me the cons/pros about this. Thank you!

9th Dec 2017, 7:06 PM
K€V
K€V - avatar
3 Answers
+ 1
Unfortunately, yes. It does have an impact on the loading speed and the significance is based on the amount of content. The more information PHP needs to parse, the slower the loading will be. But if most of your content is static, you can have a caching system to overcome this.
9th Dec 2017, 8:15 PM
CHMD
CHMD - avatar
+ 1
Simply because it's convenient (as a personal opinion). You write it once and then include it in every page you want. If you want to bring changes to the header(for example), you do not want to go through all the pages and modify the header every time. You change the header.php and consequently have the effect on all the pages wherein it was included. This applies for any other included page. It is also a way to get things organized so that you can situate yourself and know where to go if you want to add, alter, or remove something.
9th Dec 2017, 7:51 PM
CHMD
CHMD - avatar
+ 1
Good answer, that's what i kinda thought. But if you include separate files does it make your page load slower?
9th Dec 2017, 7:55 PM
K€V
K€V - avatar