when to use index.php instead of index.html
Asked Answered
C

10

34

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.

Chuddar answered 8/1, 2013 at 5:48 Comment(1)
You are over thinking it. There's no strict rule to this. If the page contains only html, you can use either because in functionality, it won't make any difference.Loney
C
58

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.

Cimmerian answered 8/1, 2013 at 5:54 Comment(8)
Very good answer, but whats about speed? Does it make your page slower if you use not necessary php code for including html code, that you could have written directly into a index.html?Af
@rave If you use the extension .html no PHP is executed.Cimmerian
yes I understand. But what I mean is: What is faster e.g. :index.html = "... <p> some text </p> ..." or index.php = "... <?php include 'somehtmlfile.html'; ?> somehtmlfile.html = "... <p> some text </p> ...". So you could write the code from 'somehtmlfile.html' directly into index.html or you can use index.php and then outsorce some html code to other files, but does this makes your page slower?Af
Of course a dynamic page is more expensive to serve than a static html page.Cimmerian
but is it significant more expensive to serve? I mean should you prefer static html instead of dynamic php, whenever you can? Or is php so fast, that the speed loss is negligible?Af
@rave This is a non-existing problem: Why you need dynamic pages use PHP, otherwise not. That's it.Cimmerian
Is this order of extension names standard?Horizon
@Horizon No standard, it's a matter of configurationCimmerian
T
9

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.

Tartarus answered 8/1, 2013 at 5:56 Comment(0)
L
8

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.

Lalonde answered 8/1, 2013 at 5:56 Comment(0)
M
7

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>
Monocyte answered 8/1, 2013 at 5:54 Comment(0)
B
6

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.

Berkshire answered 8/1, 2013 at 5:55 Comment(0)
B
4

It is beacuse sometimes you may have some logic written on index.php. Like you may check if user is logged in or not and then redirect user to some specific page. Also you may do device based redirection as in case of mobile devices.

You can always choose to create index.html but you do not know when youy may need to have some logic there.

Broomfield answered 8/1, 2013 at 5:54 Comment(0)
H
4

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

Hitlerism answered 8/1, 2013 at 6:2 Comment(0)
H
1

For example, in my index.php or another main page I mainly use php because I use variables w/ them in the rest of my site:

<?php 
     $sitepath="http://www.example.com/public";
     $author="your name here";
?>

Because I <?php echo $sitepath ?> every time I link an image in my website so it doesn't break or something. Since I'm reusing the name all the time, I use .php to be able to have that service, because if I use the name, I can change it globally.

I think that simple pages like 404.html, aboutus.html, or ViewOneBlogPost.html could be an HTML page, you might not need any functionality/variables for that.

Higdon answered 6/5, 2018 at 20:28 Comment(0)
E
0

To check current settings for file extension priority in apache2 with linux

/etc/apache2/mods-enabled/dir.conf

Eddins answered 20/1, 2016 at 19:7 Comment(0)
T
0

Well you can use HTML if you have a simple few pages where the code doesn't repeat itself a lot. But trust me, if you have a whole website in HTML with more than a thousand lines per file, you will want to use PHP. Why? Because of require and include. These will clean up your code when you need to use layouts, so you don't need to keep copying the hole code of your layout header, footer etc... It can be really difficult to maintain when you need to insert content and there's a lot of code in the way.

Here's some documentation about it

https://www.php.net/manual/en/function.include.php

Totem answered 15/9, 2020 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.