Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would I want to use .html, vs. .php, as a file extension?

Tags:

html

php

I've noticed that the .html and .php file extensions can be interchanged without apparent effect. Why would I want to use one file extension over the other?

like image 358
mohan Avatar asked Jun 08 '13 01:06

mohan


4 Answers

Use .html as a default. If your page is running phpscripts then use .php So, if you are communicating with server, use .php

like image 141
user2369405 Avatar answered Sep 28 '22 02:09

user2369405


A page ending in .php can include both HTML and/or PHP code (also javascript, css, etc inside their appropriate tags). Note that it is perfectly fine for a page without any PHP code to still have the .php extension.

However, if your page does include PHP code, the filename extension must be .php. Try it - on most web servers this won't work:

FILENAME: test.html

<?php
    echo 'Hello there';

The above page will be blank. But if you rename it to test.php, you will see the hello message.

Filename extensions are also an indicator to yourself, or other programmers, as to what type of code the file contains. It is clear at a glance that a file ending in .HTML does not contain any PHP code (especially since any PHP code contained within won't work unless the webserver config is specifically modified to allow it).

One Final Note: these days it is pleasing to have web pages that do not end with an extension at all. Of course, you cannot leave off the extension of a .php or .html page... but you can hide the extension (including the period), making the page look like it was served by Flask or React or etc. You do this via a .htaccess file (yes, exactly like that, dot and all) that sits in the root folder of your website (same folder as the index.php or index.html). See:

https://code-boxx.com/hide-php-extension-url-htaccess/

https://tecadmin.net/remove-file-extension-from-url-using-htaccess/

Here is an interesting tool to help build .htaccess files

like image 28
cssyphus Avatar answered Sep 28 '22 01:09

cssyphus


.html and .php are file extensions but the more important question is how they are run.

A .php file can run server side script and take in mysql queries and open a connection etc...all of which are server-side functions.

Html is static and only displays static content but that has now changed with HTML 5.I suggest you do a simple search to learn more about php and html and their fundamental differences.

like image 28
AlvinArulselvan Avatar answered Sep 28 '22 01:09

AlvinArulselvan


Files are handled depending on config and context. Shebangs, default programs, Apache Handler's, HTTP Headers, etc. describe handling files in various scenarios.

Executing Files In Terminal

The .php extension indicates that it is a PHP script, but the extension isn't necessary.

example-file.php

<?php
echo 'Hello World';

The script can be executed with PHP, which is clear because of the extension:

> php example-file.php

example2-file

#!/usr/bin/env php
<?php
echo 'Hello World';

With a shebang on the first line the OS can try to use the correct interpreter for the user so that the command is simplified to:

> ./example2-file

Some of the implementation details are hidden from the user by removing the file extension.

Packages often retain the extension on the source, but drop the extension during installation.

Default Programs

An extension can indicate to an OS which program to use to open a file.

Files ending in .php on my computer open in an IDE for editing whereas .html files open in a browser.

Servers and Headers

Web servers can send a file with any extension and content-type since many files don't actually exist, but are dynamically generated.

PHP web servers will serve .php files with the text/html content-type because the PHP is interpreted into text. Servers configured to return the raw PHP file as another content-type, i.e. servers not configured for PHP, will cause the web browser to download the source file rather than view the rendered file as HTML.

Since the resulting file after execution is HTML and web servers can dictate the extension, some developers decide to use .html in the URL and have them correlate to .php files to execute and return. Or the URL can not use an extension at all.

like image 34
None Avatar answered Sep 28 '22 02:09

None