Home » Php » .php with HTML versus .html with PHP

.php with HTML versus .html with PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

When I am starting to build a site that is going to require both HTML and PHP, should I be making a .html file with PHP in it (as in the file would be, say, index.html but within it there would be various tags)? Or, should I be making the files .php files and simply include HTML within it (as in the file would be, again say, index.php and it would start as PHP and I would simply intertwine HTML)?

TL;DR: Should I be weaving HTML into .php files or weaving PHP into .html files?

How to&Answers:

It should be a PHP file with HTML “weaved” into it. By default if your server sees an HTML file it does not think it needs to process scripts on the page and will render it. If it sees a PHP extension, it knows it needs to run through the PHP Processor.

You can modify your htaccess to allow HTML to be rendered through the processor, but there really is no need for you to be modding that, especially if you are a beginner.

Answer:

You use PHP files with HTML in it

Answer:

You should “weave” html into php files That way you know for sure your code will work on any server, and not just on servers that renders html files as php.

Answer:

You need to specify in your .htaccess file to be able to parse PHP inside of a .html file. The easier way to go is just to make everything .php.

Inevitably, when you get more comfortable with PHP, you’ll learn that you’ll always have a little PHP in the file (like a require or something), so best to plan for that.

Answer:

If you are new to PHP, I would recommend creating files with the .php extension, as the .php file can be executed by default. Depending on your server configuration, you may have to add some .htaccess directives to allow php code to run in an .html file.

Answer:

If you like .html extensions, you can use .phtml files for templating your system, but only for the files that containing html code. And I prefer to use .php files that containing only php code like classes etc (this is what Zend or similar libs do).