What is the difference between php and html file extensions?

19,993

Solution 1

The filetype is just a way to identify the file, you can't always trust them.

Depending on your web server configuration, you will see different results.

.html is generally use just for html with no serverside code.

.php is used for serverside php code and html if required.

They can be used for anything, it just depends on the setup.

Solution 2

You can configure your web server to handle .php and .html files differently. Your webserver is configured to interpret both as PHP. Most servers handle .php as PHP, and serve .html as-is. That is, if you put your code in an HTML file, the PHP code will not run and will show up in the output.

Some people find it nicer to have .html in the URL instead of .php. It may be useful if your users download your page and try opening them by double-clicking on them.

Solution 3

an extension is how your operating system recognizes your file and decides what to do do with it i.e. which application should it be opened with.

php is a server side scripting language. It is interpreted by a web server that has php installed on it . For eg in a XAMPP the php.exe file in XAMPP/php folder interprets the php file/commands.

HTML is the standard for sending information over the internet . So the final result of your file is a html page despite whichever serverside scripting language you use. The web server you are using will process the php commands and convert them to corresponding html and send them to your browser. The browser then processes (compiles) the html code to display you your web page.

HTML is essentially all that you see on your browser. PHP is used to interact with the web server and process information that is entered by the user into a web browser via forms or execute underlying third party scripts (such as TCL scripts) under a link to perform a automation functionality in the background hidden from the user who is using on the web site or parse a XML file or extract information from a database or maintain session information and much more.

In general PHP handles the interaction of a web application with a server that is configured to run PHP. HTML simply dumps the results in browser.

You can think of it this way- HTML is simply how your web site looks ... PHP is what makes your site intelligent so that it can interact with a user...

your getting the same result because php can be embedded in html and your web server processes both the files to give you identical results. However if you didn't have php installed on your web server you would get as output in your browser.

Solution 4

A php indicates that it is dynamically generated using PHP language. However, you don't see the page as it was originally written, but rather the end result. The end result is, in fact, an html file.

So to answer your question, to the client, a page ending in php or html will support exactly the same contents (which is to say, an html document). Even though browsers shouldn't, they often attempt to visualize tags which make no sense to them (browser interpreting <?php echo "Hello!" ?> for instance might decide "Hello" is the text to display).

Though an html really should never have php tags in it because it isn't meant to be in an html document (php documents are traslated into html documents, thus removing php tags).

Solution 5

The difference lies in how your web server is configured, or whether you need a web server at all when trying to run the files locally (ie - with them on the computer you're currently using).

For example, if you were to run both versions on a computer with no web server installed, the .html file will open in a browser just fine, though without doing anything with any PHP tags. The .php file, however, won't necessarily run and the browser may even try to "download" the file.

What the file extensions are for is to tell a computer what to do with a given extension. Just like your computer will open .doc files in a word processor, or .txt files in a basic text editor. And just like you can tell your computer to open .txt files in your word processor, you can tell the web server to handle .html files the same way as .php files (which is what yours is evidently set up to do).

Share:
19,993

Related videos on Youtube

sumit
Author by

sumit

Updated on June 02, 2022

Comments

  • sumit
    sumit about 2 years

    I am having a .php file with the following code. While I am changing the extension of the file as .html then also it is behaving in the same way. Can anyone explain the following:

    1. Why the file is behaving in the same manner with both the extensions?
    2. What is the difference between the .php and .html file extensions?

    .php file

    <html>
    
         <head>
              <!-- some html code -->
         </head>
    
         <body>
              <?php echo "Hello!" ?>
         </body>
    
    </html>
    
    • Roman
      Roman almost 13 years
      What do you mean "openning?" As in openning it in a text editor, or having it served by a webserver (like Apache)?
    • Cygnusx1
      Cygnusx1 almost 13 years
      OK, so it is as other respond, your web server treats .php and .html file the same way... so your php tags inside your html will be executed server side and put into the returning html.
  • Senad Meškin
    Senad Meškin almost 13 years
    This is configuration related, Apache configuration allows HTML files to be parsed as php, if you disable it than they won't be parsed.