What is phtml, and when should I use a .phtml extension rather than .php?

File ExtensionPhp

File Extension Problem Overview


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

File Extension Solutions


Solution 1 - File Extension

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

Solution 2 - File Extension

.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.

Solution 3 - File Extension

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.

Solution 4 - File Extension

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.

Solution 5 - File Extension

.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.

Solution 6 - File Extension

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfvgsView Question on Stackoverflow
Solution 1 - File ExtensionAlexView Answer on Stackoverflow
Solution 2 - File ExtensionQuentinView Answer on Stackoverflow
Solution 3 - File ExtensionFluffehView Answer on Stackoverflow
Solution 4 - File ExtensionCommaToastView Answer on Stackoverflow
Solution 5 - File ExtensionSoundzView Answer on Stackoverflow
Solution 6 - File ExtensionWtrndMOBView Answer on Stackoverflow