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

后端 未结 6 1866
逝去的感伤
逝去的感伤 2020-12-04 05:57

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

相关标签:
6条回答
  • 2020-12-04 06:22

    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.

    0 讨论(0)
  • 2020-12-04 06:22

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

    0 讨论(0)
  • 2020-12-04 06:24

    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.

    0 讨论(0)
  • 2020-12-04 06:30

    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
    0 讨论(0)
  • 2020-12-04 06:30

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

    0 讨论(0)
  • 2020-12-04 06:42

    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.

    0 讨论(0)
提交回复
热议问题