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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With