0

Is there a benefit of using mypage.html instead of mypage.php if there is no PHP code included inside the file?

I'm loading all pages from a folder into a element using Jquery and im curious, if there is any performance benefit by using html files instead of php file while only using HTML inside the file.

The code is simple:

$("main").load("/pages/" + hash + ".html");
Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
  • Run some tests yourself. But as far as performance goes the benefits would almost cerainly be microscopic. – Martin Jul 25 '18 at 11:46
  • Would it be still microscopic if about 100 files that "should be .html" where saved as php and thousands if not millions of people would access it? – DamianToczek Jul 25 '18 at 12:17
  • @DamianToczek I've added a couple of paragraphs to my answer regarding "micro-optimisations". – IMSoP Jul 25 '18 at 14:02
  • Related: https://stackoverflow.com/questions/8644783/html-files-as-php-in-nginx – Jesse Nickles Sep 07 '22 at 10:37

5 Answers5

2

The web server will generally decide what to do with the file based on its "extension" (i.e. the last part of the file name); there are lots of variations that are possible, but the common default configuration would be:

  • File names ending .html will be read directly by the web server, and sent back to the browser with a Content-Type of text/html, regardless of their content.
  • File names ending .php will trigger execution of a PHP interpreter, as a module, a CGI script, or a FastCGI proxy. The PHP interpreter will then read the file, send a Content-Type of text/html by default, and output any file content not between <?php (or <?=) and ?> directly as though told to echo it.

If you have a fully-trusted HTML page with no instances of <? inside, the only difference will be a small performance advantage in the web server reading it rather than passing control to a PHP interpreter. The exact overhead depends on exactly how it's running - CGI mode is very slow, but rarely used; a modern configuration with FastCGI and OpCache would add very little delay.

The only other difference would be if someone could edit the HTML file and accidentally or maliciously include code that would be interpreted by PHP. By saving it as .html, and checking your server configuration, you can be sure that this code will not execute.

Note that these kinds of small differences are sometimes referred to as "micro-optimisations", and the general advice is to only look at them when you've exhausted all other optimisations, and to only optimise based on real benchmarks of your application. Even if millions of people request this page, each of them might only see a few milliseconds improvement from this change, and your server would see a tiny fractional reduction in CPU usage. But a single database query might be taking hundreds of times that, and implementing a more efficient query, or an effective cache, would give you far more of a performance boost.

If you find that these files really are causing a measurable delay, you should instead commit more effort to finding the best solution, rather than only making a small change. Since these are static files, you could look at using a separate CDN (e.g. CloudFlare, Akamai) or hosting them on a separate server tuned specifically to serve these files, and not fighting for resources with your dynamic content.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
1

There should be no difference in using either file extension.

Depending on the server used, it is possible that you have a little performance improvement if the server does not look for php tags in .html files. If it exists, it must be negligible but you could benchmark it.

NanoPish
  • 1,379
  • 1
  • 19
  • 35
1

The end result will be the same, regardless. That said, there's a very slight performance benefit to using .html instead of .php, because the server won't need to execute the php interpreter.

With most modern servers, php doesn't need to start, as the process is often already in memory, however there's that just very-very slight difference of actually parsing the file and verifying that there's no php to execute in it.

It's not a difference I would worry about.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

I'd say it's a difference since your webserver isn't starting the PHP interperter for it. But you won't benefit much from it.

maio290
  • 6,440
  • 1
  • 21
  • 38
  • 1
    PHP Interpreter is only started when web server see's ` – RiggsFolly Jul 25 '18 at 11:46
  • 3
    @RiggsFolly You are wrong. Web server does not parse the content of the file. It will invoke php interpreter for all php files as defined in the server configuration (usually extension .php) PHP interpreter will then look for ` – Aleks G Jul 25 '18 at 11:49
  • Can you provide any source for that? I always guessed that that's the difference here (not having any source). – maio290 Jul 25 '18 at 11:49
  • 4
    @RiggsFolly — It is the PHP interpreter that looks for ` – Quentin Jul 25 '18 at 11:50
  • Well that was not my understanding, but I am more than happy to have learned something here @Quentin – RiggsFolly Jul 25 '18 at 11:52
0

no difference between the performance while changing the .HTML to .PHP or vice versa, however when tested on localhost .PHP extension in localhost runs HTML content too but not vice versa.

Muhammad Ali
  • 956
  • 3
  • 15
  • 20