Jump to content
Larry Ullman's Book Forums

General Insight Into File Extension Naming (.Php Or .Html) And Its Effects On Functioning?


Recommended Posts

Hi, Larry. If you're inclined, I'd really appreciate your insights regarding .html or .php file extension naming, and how the different extension names (with identical internal php code) effect functioning? I've been able to have PHP functions work with either extension name in previous personal projects, however I'm not exactly clear on standing practices / naming conventions? Also, why can't external CSS files be referenced with the header.html? For an example, could I change this file to header.php, and with a few minor edits have it functioning the same and referencing external CSS sheets? Thanks is advance.

Link to comment
Share on other sites

Well, the first factor is whether or not the file is being accessed directly or not. If the file is being accessed directly (i.e., loaded in the browser), then the extension matters, because only .php extensions are treated as PHP by the server (unless the server is set to parse .html files as PHP, too, which is rarely the case).

 

If a PHP script is including another script, it mostly doesn't matter what extension the included file has, because it's the parent script (the one being accessed directly) whose extension counts.

 

If a file has mostly HTML and is used mostly to generate HTML, even if there's a bit of PHP in it, I tend to use .html as the extension. If the file has mostly PHP and is used mostly for executable PHP code (i.e., as an included file), I use .php.

 

As for the CSS issue, you can reference external CSS files whether it's header.html or header.php. Makes no difference.

Link to comment
Share on other sites

 Share

×
×
  • Create New...