MayfieldGlobal

Everything from operating systems, programming to web development and more.

Jan
19

How do I use PHP includes in my Web pages?

Posted under PHP by westminster

PHP Includes allow you to easily reuse specific sections of code and content throughout your site. For example, many sites have a footer section where they display additional navigation links, ads, etc. Without PHP Includes (or other server side programming language), a developer would need to write the same code for this footer into each and every page of their site. Manually coding every page with the same identical content is a serious hassle and can make maintenance a nightmare. In comes PHP includes.

A basic include statement is written like this:<?php include ( 'directory/filename.php' ); ?>

Pretty simple. Now, let’s say you want the links listed below to be displayed as the footer in several pages of your site:

  1. Using your text editor, create a new file named sitefooter.php. Copy and paste the following code into footer.php:<div id="sitefooter">
       <ul id="sitefooterlinks">
           <li><a href="#">home</a></li>
           <li><a href="#">services</a></li>
           <li><a href="#">products</a></li>
           <li><a href="#">about us</a></li>
           <li><a href="#">contact us</a></li>
        </ul>
    </div>
  2. Save the file to a folder named: includes (or whatever you want).
  3. Copy and paste the following code into each HTML page that you want the navigation links to appear:<?php include ( 'includes/sitefooter.php' ); ?>Note: Placement of the include statement is crucial. You must paste the statement at the exact location in your HTML code where you want the menu to appear.
  4. Upload the includes folder and any HTML files that now have your include statement to your PHP enabled Web server. To establish a local development environment in which to test your code, try XAMPP.
  5. Navigate to a page in your site that contains the include to view the results. Since the includes are magically executed on the server side, you will only see the contents of your include file (i.e. the div and its contents from our example) when viewing the source of your live HTML document; not the actual include statement.

Anytime you need to make a change to the footer links, you now only need to change one file; footer.php. Your changes are automatically reflected in every page that you pasted the include statement.

An easy way to build your site to incorporate includes, is to first create a few pages using plain old HTML. Once you have done so, review the pages for similarities (i.e. sections that are repeated) in either code or content. Cut the repeated sections out and place them into their own include files. Then simply replace the cutouts with include statements that reference the appropriate PHP files.

This tutorial barely scratches the surface of PHP includes’ power and flexibility. Includes can contain virtually any content you desire. You can even layer includes by nesting one inside the other. In fact, an entire site can be built using almost nothing but includes. For additional reading, try Google.

Add A Comment