when to use index.php instead of index.html

2019-01-13 08:11发布

I am relatively new to php. There is a very basic thing that has been troubling me. I understand that php is used to make web sites dynamic. I also understand that php is one of the many server side scripting languages that can be used to make dynamic web sites.

However, what I do not understand is that when do i need to use an index.php page. Say for example if i have just a simple login page on my index page, it could very well just be a simple html page as well. Right? Then why would i want to make it index.php instead of index.html?

An example of a sample situation would be great.

10条回答
太酷不给撩
2楼-- · 2019-01-13 08:45

By default, the apache server needs a file with .php extension to parse the PHP code inside it. Though, if you wish, you may configure your server, by adding a single line to the configuration file, to use files with any extension with PHP code inside it. You can edit the apache by yourself to support php also in .HTML extension.

查看更多
甜甜的少女心
3楼-- · 2019-01-13 08:45

In simple terms, you can easily access index.html file and get the data beneath it.But index.php is difficult to access. For your simple application index.html will do the trick. If you are planning for some big and secure application go for index.php

查看更多
Evening l夕情丶
4楼-- · 2019-01-13 08:50

You can use which-ever you prefer: If you prefer keeping forms and basic pages that don't use data in HTML, and keep pages that use php in php format that is fine.

But one method I and I assume most others use is just make all of your pages php files. This is because you can include a html document in the php file and display it just the same. But you cannot do php queries from a html file so it is easy to just always use php just incase you want to add some php scripts to it.

Index.php:

<?php
$var = 5;
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h2>Your variable was <?php echo $var; ?></h2>
</body>
</html>
查看更多
甜甜的少女心
5楼-- · 2019-01-13 08:50

Its not a big deal whether you use index.php or index.html. You ca use anyone of either. Only thing is you need PHP(or any other server side scripting language) to make your site dynamic.

Like you have a login page,you can surely make it as inde.html but your logics would either have to be in a different file or embedded in HTMl.

查看更多
时光不老,我们不散
6楼-- · 2019-01-13 08:54

I also have the same problem mate. But, I think .php is much better than .html since it is more dynamic plus you can do a bunch of cool stuff also.

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

You will have to choose the PHP extension (.php) when you want php code to be executed in the file. PHP code is code between the opening <?php or <? and the closing ?> tags.

When no PHP code should be executed you can use the .html extension.

Usually when using the .php extension you are telling the web server, that it should use a php interpreter to process the file before it will be delivered to the browser. The php interpreter will then replace all content between the <?php and ?> by the output of the PHP code. Just as if you wrote it manually. The processed file will then be delivered to the browser.

However, using the .php extension to tell the web server to process php code is configurable. If you want you can use other file extensions too.

There is another thing that should be pointed out. When you only type the url path (without a filename) like :

http://www.myserver.com/

there is an order of extensions (filenames) which the webserver (apache) searches for an index document. For example an apache config may contain a section like:

<IfModule mod_dir.c>
      DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Meaning that the index document is searched in the order above. This means if you place an index.html and a index.php in the same folder - and having the configuration above - always the index.html would be delivered by the server.

查看更多
登录 后发表回答