How to Include HTML in Many Documents Using PHP

website plan

Stockbyte / Getty Images

Many developers use PHP to include pieces of website content that repeat across an entire site: typically, the site's header, including navigation elements and logo, as well as the footer, social media widgets or buttons, and other content. This is a web design best practice. It helps minimize loading time and optimize the viewing experience. Once site visitors understand one page, they have a good idea how to navigate the others.

Without using PHP "includes," you would have to add these individually to each page. This poses a problem when you want to make a change, too. For example, to update a copyright date in the footer or ad a new link to your site's navigation menu, you'd have to change every page on the website. For large sites, a simple edit becomes a time-consuming, repetitive task.

The PHP "Include" Solution

If you have PHP on your server, you can write one block of code and include it wherever you like — on every page or selectively. For example, say you have a "contact us" form widget that allows site visitors to connect with your company. If you want this to appear on certain pages but not on others, using a PHP include is a timesaving solution. Should you need to edit that form in the future, you'd merely edit that one block of code in one file, and every page that includes it would get the update. 

Most servers are configured with PHP installed. Contact your system administrator or host if you're not sure whether PHP has been installed on your server. If it has not, they can help you with the installation.

  1. Write the HTML you want to repeat on multiple pages and save it to a separate file. In this example, we're including a contact form on select pages and naming it contact-form.php.

    Save all your include files in a separate directory named "includes" (as in this example) or something similar. You'll know where to find them and how to call them.

  2. Open one of the web pages on which you want the contact form to display.

  3. Place the following code exactly where you want the form to display. Change the path and file name as appropriate.

    require($DOCUMENT_ROOT . "includes/contact-form.php");
  4. Write this same code to every page on which you want the contact form to appear.

    Just copy and paste the code for speed and convenience.

  5. If you want to change something in the contact form (for example, add a new field), simply edit the contact-form.php file. When you uploaded your edited file to the includes/ directory on the server, you'll see the change reflected on every page of your site that uses this code. This is much faster than having to change those pages individually!

Anything that can go in a standard HTML file can go in a PHP include.

Save any page that uses a PHP include as a PHP file with the appropriate extension (e.g., index.php). Some servers don't require this, but making this a practice removes any uncertainty and avoids problems should you move your site to another server.

mla apa chicago
Your Citation
Kyrnin, Jennifer. "How to Include HTML in Many Documents Using PHP." ThoughtCo, Sep. 30, 2021, Kyrnin, Jennifer. (2021, September 30). How to Include HTML in Many Documents Using PHP. Retrieved from Kyrnin, Jennifer. "How to Include HTML in Many Documents Using PHP." ThoughtCo. (accessed March 29, 2023).