Is there a difference between saving your HTML files with PHP content as *.php
files or as *.html
files?

- 2,214
- 1
- 17
- 38
-
2No. Both are just text files. – PeeHaa Oct 29 '13 at 15:56
-
duplicated? http://stackoverflow.com/questions/6776832/what-is-the-difference-between-php-and-html-file-extensions/6776917#6776917 – Ricardo Binns Oct 29 '13 at 15:56
-
It's all binary to me. – Josh Crozier Oct 29 '13 at 16:01
-
**TRICK question!** – *"Don't fall for it!"* ;-) – Funk Forty Niner Oct 29 '13 at 16:04
3 Answers
Yes. Unless you've set your webserver up to serve html
files through the PHP interpretor, these will never talk to PHP, which means it'll output raw PHP code.
By most default configurations
<div><?php echo 'test'; ?></div>
Would output the following:
PHP (.php
and sometimes .php3
/.php4
/.php5
/.phps
):
<div>test</div>
HTML:
<div><?php echo 'test'; ?></div>
The HTML file will display as (empty)
in your browser, but if you look at your source you can see the raw PHP code.
It depends on the config of your web server, but generally speaking, yes there is: the default settings for most web servers means that a .php
file will be processed as PHP, whereas a .html
file will be sent directly to the browser without any processing.
You can change this behaviour if you really want to, but it's probably a bad idea.
If you're trying to make your URLs more "friendly" by doing this, then it's definitely not the best approach. It won't really change anything other than the file extension; you'll still have ugly URL parameters, etc that your users will see.
As an alternative, you may consider using URL rewriting (ie mod_rewrite) to convert "friendly" URLs entered by your users into .php
URLs with URL parameters to be processed by your code.

- 166,037
- 39
- 233
- 307
you can't execute PHP code from an .html
file

- 433
- 2
- 7
-
5[Not true](http://stackoverflow.com/questions/11312316/how-do-i-add-php-code-to-html-files/11312349#11312349) – John Conde Oct 29 '13 at 15:57
-
1Not by default, anyway. You can configure your server to do so if you wanted to. – gen_Eric Oct 29 '13 at 15:57
-