Link to home
Start Free TrialLog in
Avatar of Jack Andrews
Jack AndrewsFlag for United States of America

asked on

Required file extension when using php includes

I have an html page and I want to use php includes in sections of it. Do I need to change the file extension from .html to .php for this to work?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to be clear:

1. The page that is doing the INCLUDING needs to be PHP. The file extension doesn't matter on the pages that you INCLUDE.

2. While 99% of the time the file extension for a PHP script is .php, it -can- be configured to be something else. I've seen some web hosts set up .html to be processed as PHP, but it's usually not a good idea.

3. The file extension alone does not make it a PHP script. There is a PHP engine, which is sort of like an oven. You put PHP code in, it bakes, and you get the resulting HTML back out. The web server has to be told which files to pass to that engine. That's usually where the file extension comes in - most web servers are just configured so that if a request comes in for a file that ends in .php, then it passes that script over to the PHP engine to run.

However, if you don't have PHP installed, then the file extension itself will not do anything. There's no magic to the .php file extension by itself - it's just a handy identifier for the web server (and helps us humans know what kind of code is inside the file).
Hi,

This is also possible to include code using Javascript.
I always use PHP to include content but if you could not use PHP for a reason...

You can embed a html file on the page using Javascript / jQuery.

Here is a way to include a file nav.html on the page
 

HTML part
<div  id="mymenu"> </div>

Open in new window


Javascript part to load nav.html
$(function(){
          $('#mymenu').load('nav.html', function() {
           // some code here may be required
           });
    });

Open in new window



Reference jQuery https://api.jquery.com/load/
Avatar of Jack Andrews

ASKER

Thanks. I assumed that's the case. Good explanations.