什么是 phtml,我什么时候应该使用 .phtml 扩展名而不是 .php?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11859015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is phtml, and when should I use a .phtml extension rather than .php?
提问by fvgs
I'm wondering what the difference between .phtml and .php files is, and when to use one over the other.
我想知道 .phtml 和 .php 文件之间的区别是什么,以及何时使用一个而不是另一个。
回答by Alex
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.
就页面渲染而言,通常没有区别。但是,当您的 Web 项目变得更大时,这是一个巨大的设施开发人员。
I make use of both in this fashion:
我以这种方式使用两者:
- .PHPPage doesn't contain view-related code
- .PHTMLPage contains little (if any) data logic and the most part of it is presentation-related
- .PHP页面不包含与视图相关的代码
- .PHTML页面包含很少的(如果有)数据逻辑,并且大部分是与表示相关的
回答by Quentin
.phtmlwas the standard file extension for PHP 2 programs. .php3took over for PHP 3. When PHP 4 came out they switched to a straight .php.
.phtml是 PHP 2 程序的标准文件扩展名。.php3接手 PHP 3。当 PHP 4 出来时,他们切换到了一个直接的.php.
The older file extensions are still sometimes used, but aren't so common.
有时仍会使用较旧的文件扩展名,但并不常见。
回答by Fluffeh
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.
它是一个文件 ext,有些人用了一段时间来表示它是 PHP 生成的 HTML。因为像 Apache 这样的服务器并不关心你使用什么作为文件 ext,只要它映射到某些东西,你可以继续调用你所有的 PHP 文件 .jimyBobSmith,它会很高兴地运行它们。PHTML 恰好是一种流行了一段时间的趋势。
回答by CommaToast
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.
举例说明 Alex 所说的话,例如,如果您使用 Magento,.phtml 文件只能在 /design 区域中作为模板文件找到,并且包含 HTML 和 PHP 行。同时,PHP 文件是纯代码,其中没有任何 HTML 行。
回答by Soundz
.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.
.phtml 文件告诉网络服务器那些是由服务器生成的具有动态内容的 html 文件……就像浏览器中的 .php 文件一样。因此,在高效使用中,您应该不会遇到从 .phtml 到 .php 文件的区别。
回答by WtrndMOB
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.
如果您正确设置了 Apache,您可以选择世界上任何扩展。如果您在 Apache 配置中进行设置,则可以使用 .html 来执行 PHP。
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)
总之,扩展与应用程序或网站本身无关。你可以使用你想要的,但通常使用 .php(不要重新发明轮子)
But in 2019, you should use routing and forgot about extension at the end.
但是在2019年,你应该使用路由,最后忘记了扩展。
I recommend you using Laravel.
我推荐你使用 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.
回答@KingCrunch:是的,Apache 默认不使用它,但如果您更改配置,您可以轻松使用它。但这并不推荐,因为每个人都知道这不是一个真正的选择。
I already saw .html files that executed PHP using the html extension.
我已经看到使用 html 扩展名执行 PHP 的 .html 文件。

