266

I'm wondering what the difference between .phtml and .php files is, and when to use one over the other.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
fvgs
  • 21,412
  • 9
  • 33
  • 48

6 Answers6

357

There is usually no difference, as far as page rendering goes. It's a huge facility developer-side, though, when your web project grows bigger.

I make use of both in this fashion:

  • .PHP Page doesn't contain view-related code
  • .PHTML Page contains little (if any) data logic and the most part of it is presentation-related
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Alex
  • 23,004
  • 4
  • 39
  • 73
  • 93
    Alright, so using the .phtml extension rather than .php would just be a formality used to denote a file that is more html dense rather than php. – fvgs Aug 08 '12 at 06:50
  • 14
    Precisely. Leveraging the fact that a different file extension can be associated with a different icon, you can also tell them apart effortlessly (working with tens or hundreds of files, just organizing them in folder doesn't always cut it). – Alex Aug 08 '12 at 06:54
  • There's also .phps PHP source file. – CMCDragonkai Dec 09 '13 at 15:48
  • does `.PHTML` still work. i wrote the following piece of code and saved it with .phtml and it did not run the php part `
    everybody
    `
    – dhpratik Apr 10 '14 at 07:23
  • 2
    @dhpratik Check your server configuration, the extension is probably not being handled properly. If configuration seems okay, that's better off as a SO question on its own – Alex Apr 10 '14 at 10:53
122

.phtml was the standard file extension for PHP 2 programs. .php3 took over for PHP 3. When PHP 4 came out they switched to a straight .php.

The older file extensions are still sometimes used, but aren't so common.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
38

It is a file ext that some folks used for a while to denote that it was PHP generated HTML. As servers like Apache don't care what you use as a file ext as long as it is mapped to something, you could go ahead and call all your PHP files .jimyBobSmith and it would happily run them. PHTML just happened to be a trend that caught on for a while.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • 29
    +1 for .jimmybobsmith I might do that once to confuse a lot of people on the team who doesn't know this. – o_O Mar 04 '14 at 08:18
  • You have to register the extension in your apache.conf/httpd.conf though... another trick is to `include` the file with any extension from php, it will run as php anyways. – SparK Jan 18 '19 at 18:22
20

To give an example to what Alex said, if you're using Magento, for example, .phtml files are only to be found in the /design area as template files, and contain both HTML and PHP lines. Meanwhile the PHP files are pure code and don't have any lines of HTML in them.

CommaToast
  • 11,370
  • 7
  • 54
  • 69
10

.phtml files tell the webserver that those are html files with dynamic content which is generated by the server... just like .php files in a browser behave.

So, in productive usage you should experience no difference from .phtml to .php files.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Soundz
  • 1,308
  • 10
  • 17
  • 7
    `phtml` were usually never directly accessed. Furthermore `phtml` is usually not configured as valid PHP-extension in most webservers. – KingCrunch Aug 08 '12 at 06:54
0

You can choose any extension in the world if you setup Apache correctly. You could use .html to do PHP if you set up in your Apache config.

In conclusion, extension has nothing to do with the app or website itself. You can use the one you want, but normaly, use .php (to not reinvent the wheel)

But in 2019, you should use routing and forgot about extension at the end.

I recommend you using Laravel.

In answer to @KingCrunch: True, Apache not use it by default but you can easily use it if you change config. But this it not recommended since everybody know that it not really an option.

I already saw .html files that executed PHP using the html extension.

WtrndMOB
  • 29
  • 2