PHP Include for HTML?


I have a navigational bar, an image, and a heading that I'll be including in every page of my website, so I wanted to use php include to refer to this code in several pages. However, I think I may have the syntax wrong or something because it's not rendering anything when I load it. Here are some code snippets:

<!-- sample page --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
<?php include ('headings.php'); ?>
<?php include ('navbar.php'); ?>
<?php include ('image.php'); ?> 



    echo '<ul id="nav">
            <a href="Home.html">Home</a>
            <a>About Me</a>
                    <a href="Career.html">Career</a>
                    <a href="Coding.html">Coding</a>
                    <a href="Personal.html">Personal</a>
            <a href="Travels.html">Travel</a>
            <a href="Contact.html">Contact</a>


Thanks for helping!

This question is tagged with php html include

~ Asked on 2012-08-09 16:03:58

The Best Answer is


You can use php code in files with extension .php and only there (iff other is not defined in your server settings).

Just rename your file *.html to *.php

If you want to allow php code processing in files of different format, you have two options to do that:

1) Modifying httpd.conf to allow this for all projects on your server, by adding:

AddHandler application/x-httpd-php .htm .html

2) Creating .htaccess file in your separate project top directory with:

<Files />
    AddType application/x-httpd-php .html

For second option you need to allow use of .htaccess files in your httpd.conf, by adding the following settings:

AllowOverride All
AccessFileName .htaccess

*that is correct for Apache HTTP Server

~ Answered on 2012-08-09 16:07:22


You don't need to be echoing the info within the php file. A php include will automatically include any HTML within that file.

Make sure you're actually using a index file with a .php extension, .html won't work with php includes. (Unless you're telling your server to treat .html files otherwise)

Make sure your paths are correctly set up. From your description, the way you've set it up your header.php/navbar.php/image.php files should be in your root directory. So your root directory should look like this:


Otherwise if those PHP files are in a folder called /includes/, it should look like so:

<?php include ('includes/headings.php'); ?>

~ Answered on 2012-08-09 16:09:55

Most Viewed Questions: