1

Is it ok if I include many PHP file within a PHP file? What is the best practice? Or where can I learn the best basic practice of writing PHP?

Why I do this

Is because I often can change the content of each div through the PHP file instead of directly change in the main PHP file.

test

3
  • In the abstract there is nothing wrong with including multiple PHP files to build the page, but if you are doing this many times, it may indicate a poor design. What do content 2.php etc. actually contain?
    – user82096
    Commented Nov 3, 2016 at 9:21
  • content2.php will contain all the notifications from administrator. Basically it is just a <div class="flashnews" style="hidden"></div> when needed I will push some news to it. Commented Nov 3, 2016 at 9:29
  • content5.php is the footer. By doing this way I can dynamically change the content5.php to push all the changes to all the mainpage. Commented Nov 3, 2016 at 9:30

1 Answer 1

3

Based on the level of information here, this is fine, and it's standard practice in PHP to divide parts of your page into different files. Major PHP content management systems like WordPress do this.

Having sections of the page like header, menu, and footer in their own files is commonly used, as it allows you to easily reuse that code on multiple pages.

If you have a section that is only used on one page, and will only ever be used there, a separate file is not needed. But even then, it isn't clearly harmful. It may helpfully divide your code into more manageable chunks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.