Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Jenny Swift
Jenny Swift
21,999 Points

Can HTML files be included with PHP?

Hi, I'm new to PHP. Is it possible to include HTML files with PHP, for example like this: <?php include('includes/menu.html'); ?>

I saw something like this at css-tricks.com, but I haven't yet got it to work, whereas if I change my file extensions from .html to .php things work fine.

If it is not possible to include html files with php, I'm guessing that means if I had a website with a navigation bar that I wanted on all my pages I would then need to change every one of my html files to php files? Are there any drawbacks to doing this please?


Yes, you can include HTML files using the php include statment, BUT----the file you're including (via the include statement) must have a .php extension. In other words, you could have a file consisting of nothing but HTML, but it'd need a .php extension to work.

3 Answers

Here's a link to the php include documentation, there you can read all about php include: http://www.php.net/manual/en/function.include.php

As for CSS-tricks, it could be a misprint or copy-paste error, it happens :)

You can have HTML in a .php file, so changing the fileextention of your html-file to .php is a good option. It doesn't have to have any php in it. In fact in most php projects the html files usually have the file extension .php even though they contain mostly HTML.

I don't think it's possible to do a <?php include('includes/menu.html'); ?>

Jenny Swift
Jenny Swift
21,999 Points

So if it isn't possible to include an html file that has an html extension, can anyone please explain why http://css-tricks.com/including-navigation-code-on-all-pages-even-with-current-navigation-highlighting/

says this: "The classic menu HTML which you can transform into any number of beautiful things through CSS. You could include that in all your pages by putting it into it's own HTML file (like "menu.html") and including it on your pages with some kind of simple include statement like this PHP snippet:

<?php include_once("menu.html"); ?>"