devirkahan
devirkahan

Reputation: 460

.php with HTML versus .html with PHP

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?

Upvotes: 8

Views: 2263

Answers (6)

Kerem
Kerem

Reputation: 11576

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).

Upvotes: 0

Daniel Beacham
Daniel Beacham

Reputation: 103

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.

Upvotes: 5

James L.
James L.

Reputation: 4097

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.

Upvotes: 0

PlantTheIdea
PlantTheIdea

Reputation: 16359

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.

Upvotes: 0

Green Black
Green Black

Reputation: 5084

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.

Upvotes: 0

Paul Dessert
Paul Dessert

Reputation: 6389

You use PHP files with HTML in it

Upvotes: 1

Related Questions