Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:19:34+00:00 2026-05-30T00:19:34+00:00

Is it bad practice using require() a lot? Most websites have a header and

  • 0

Is it bad practice using require() a lot? Most websites have a header and a footer that looks the same whereever you navigate on the page. But the main content varies from page to page. And since I don´t want to reuse code, have the same code in two places, I think require() is pretty good. But is it okay using it like I do?

Here´s my example:

<div class="content">
    <section>
        <?php require('mainpage.php'); ?>
    </section>
    <aside class="sidebar">
        <div class="sidebar-box">
            <?php require('sidebar/box1.php'); ?>
        </div>
        <div class="sidebar-box">
            <?php require('sidebar/box2.php'); ?>
        </div>
    </aside>
</div>

A sidebar box is a box that contains stuff like “Latest news” etc. And since I may want to have the “Latest news” box on many pages I need to require() it in order to not have the same code in two places.

Should I solve it someway else?

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:19:36+00:00Added an answer on May 30, 2026 at 12:19 am

    Nothing wrong with that at all, it’s very good practice to reuse code chunks. It also keep the HTML structure far more readable and gives you some basic separation between HTML presentation and PHP business logic. Check out the MVC pattern for why this is a good thing.

    Some points to bear in mind:

    • You might want to consider using a $docroot variable that has the full server file path, as relative file paths can occasionally go wonky on some PHP installs and also if you include the above code itself from another file. Relative file paths look like it’s easier to move the whole codebase around, but you can just alter $docroot if you do this and it’s fine. You could also use a $librarydir variable so that your HTML templates can be moved around without shifting the location of the PHP files.
    • Some files ought to be brought it with require_once() so that you don’t get a mess e.g. the HTML header, page footer etc. Might sound obvious, but it’s easy to put the footer into two files that you then include from somewhere else, so this can make debugging easier by giving a more verbose error message.
    • You may find that a templating engine which does the MVC approach in a more formalised an flexible way could help you. Check out Smarty as others have mentioned, or Dwoo, which is a PHP5 rewrite of it.
    • Odd things can happen if the code in the included files uses variables in the global scope, and as your program becomes more complex, this can lead to hard-to-pin-down bugs where one include is subtly breaking another by altering variables with the same name. Consider putting the code for a sideblock into a function or class so that variables are in their own scope and you can keep them separate.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A number of times I've argued that using clone() isn't such a bad practice.
Is it considered bad practice to have multiple views for same URL in MVC,
I heard that using System.out.println for logging purposes is a very bad practice and
Some people claim that using namespace std; is bad practice. Others say it's OK
I was reading that using anonymous functions in javascript is bad practice, because it
Is it bad practice to have a string like name=Gina;postion= HouseMatriarch;id=1234 to hold state
I know they are bad practice but I have a real need on an
Is it considered bad practice to have multiple try's in one method and structure
So I have a helper method that looks something like the following: private D
Assuming I'm not using the lock statement, is this a bad practice? public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.