What is the difference between php and html file e

2019-02-09 06:46发布

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>

10条回答
放我归山
2楼-- · 2019-02-09 07:05

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-09 07:06

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

查看更多
ゆ 、 Hurt°
4楼-- · 2019-02-09 07:07

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.

查看更多
Root(大扎)
5楼-- · 2019-02-09 07:11

PhP is server side.

HTML is client side.

Plus, on the web, filetypes mean nothing. They are overridden by the !DOCTYPE declaration.

查看更多
干净又极端
6楼-- · 2019-02-09 07:12

As far as I Know, depending on the extension the web server will process your file on one way or another. Also, for example, you could have a PHP file that doesn't generate any HTML output, but it redirects to another file.

If you want to give a *.html ended page, you could do it programatically.

查看更多
混吃等死
7楼-- · 2019-02-09 07:13

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.

查看更多
登录 后发表回答