Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use index.php instead of index.html

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.

like image 274
qre0ct Avatar asked Jan 08 '13 05:01

qre0ct


People also ask

Can I use php instead of html?

PHP is used for server-side development. PHP can't be used in an HTML file. HTML can be used in a PHP file.

Why do we use index php?

php extension to tell the web server to process php code is configurable. If you want you can use other file extensions too. Meaning that the index document is searched in the order above. This means if you place an index.

Is it necessary to have index html?

The reality is that a web server can be configured to recognize any file you want as the default for that site. That being the case, it's still a good idea to stick with index. html or index. htm because it is immediately recognized on most servers without any additional configuration needed.


2 Answers

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.

like image 111
hek2mgl Avatar answered Sep 22 '22 15:09

hek2mgl


It will not hurt a website if you serve every page as .php. Apache is very fast to serve any php, let alone one which contains only static html.

As a beginner you may find php will benefit you, by allowing you to creating simple templates. Site header and footer includes for instance could be written in one file, then included in all the other pages.

like image 24
LessQuesar Avatar answered Sep 21 '22 15:09

LessQuesar