Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

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?

Jenny,

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"); ?>"