6

I do have an index.php file which includes different parts of the page via PHP.

For example this includes the <head> section of the HTML page into the index.php file:

<!doctype html>
<html>
    <head>
        <?php include($_SERVER['DOCUMENT_ROOT']."/PATH-TO-FILE/head.php"); ?>
    </head>

    <body>
        ...
    </body>
</html>

The content of "head.php" may be something like this:

<meta charset="utf-8">
<title>Title of page</title>
<meta name="description" content="Description text...">

...

<link rel="stylesheet" href="stylesheet.css">

...

and so on...

Technically "head.php" is not a PHP file because it does not contain any PHP code. Neither is it a valid HTML document. It is just a HTML code fragment.

Until now I have always named these HTML code fragment files either *.html or *.php.

My question is whether this is correct or not?

Is it advisable to not give it a file extension at all? Instead of "head.php" simply "head"?

Please keep in mind that certain directives can be set in the server's .htaccess file concerning the caching and expiration of *.html and *.php files. Removing the file extension and renaming it from "head.php" to "head" may exclude it from the mentioned directives in the .htaccess file.

While searching I found this question here on StackOverflow: What extension should I use for files containing fragments of HTML

But that question was asked 6 years ago and there are so many different file extensions mentioned there, it's difficult to say which one to use.

Can anyone give an updated answer concerning this issue?

Community
  • 1
  • 1
DSC
  • 63
  • 4
  • 1
    How do you think to change the title of different files? Maybe it's better to pass a parameter to the PHP function with the name. In html is important the title. – Braisly Sep 07 '15 at 17:27
  • I am not concerned about the tile of anything in this case. I am just asking how to name the file itself. I am asking about the file extension which such files should have. Is there a naming convention that covers this issue? – DSC Sep 07 '15 at 17:35

2 Answers2

4

If you use include, I'd strongly recommend using *.php. include executes the code in it, even if it's raw HTML. (in that case it's just output without much further processing)

Hence, use *.php for files output that way. (Else you might get also bad highlighting in editors when using <?php one day in it for some reason; also, if someone opens the *.html file directly, he'll see the raw code then)

In case you are using e.g. readfile(), then use *.html to highlight that it is raw HTML code only, nothing ever will be executed.

It basically depends on how you include the file.

P.s.: To no extension at all. Not really advisable, that's usually what you use for binary files and directories. Note that extensions really just are to give you oversight what happens.

bwoebi
  • 23,637
  • 5
  • 58
  • 79
2

I would leave as php. Your are including that file with php. Itself head file is useless as its own.

What if you add some php to that file, for further development? Add php extension back not an efficient lifecycle.

More importan is where you store those files. Putting them under a templates folder will tell to anyone, that those files are templates.

Iamzozo
  • 2,341
  • 18
  • 21
  • Thanks, I usually put them into a folder called "includes". – DSC Sep 07 '15 at 17:50
  • Where do you put your classes, helpers, third party libraries? – Iamzozo Sep 07 '15 at 18:06
  • I use a folder structure for the different modules and libraries if present. Classes do not fall into this category. They are defined in the stylesheet. But maybe you mean something else when you say "classes". – DSC Sep 07 '15 at 18:16