在 PHP 中使用常规 HTML 和在 echo 语句或变量中使用 HTML 之间的区别
你好,我一直在思考哪种是在 MVC 架构中编写 html 的最佳方式。它会是常规的 html
或 echo '
。这是一个非常小的示例,但有时我们可以使用具有多行代码的单个变量或单个 echo 语句。
我们是否会遇到任何性能问题,因为 php 必须由网络服务器(apache、iis 等)解析,而 HTML 会消失并直接由浏览器呈现。那么使用常规 HTML 会节省解析时间吗?
Hello I have always been thinking which is the best way of writing html in an MVC architecture.Would it be regular html as <div id="id1"> <input type="text" name="tBox" /></div>
or echo '<div id="id1"> <input type="text" name="tBox" /></div>';
. This is a very small example, but sometimes we can have a single variable or single echo statement having multiple lines of code.
Would we have any performance issues since php has to be parse by the webserver (apache,iis etc), while HTML gets away and is rendered directly by the browser. So would we gain any parsing time by using regular HTML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
常规的直接 HTML 更可取,因为它可以静态提供,无需 PHP 处理。然而,也就是说,如果您有 MVC 架构,那么您不应该首先回显 HTML —— 该输出应该由您的视图层处理。
Regular straight HTML is preferable as it can be served statically without processing by PHP. That said however, if you've got an MVC architecture, then you shouldn't be echo'ing HTML in the first place -- that output should be handled by your view layer.
当文件是 php 文件(并且在某处包含 php 代码)时,这根本不重要。无论里面有什么,服务器都会执行它,并按原样返回纯纯文本,并在两者之间添加回显的文本(在回显位置)。最后,生成一个连贯的纯文本并将其发送到客户端(具有正确的 mime 类型),然后让浏览器对其进行解释。
我通常在
标签之外放入较长的 static html 块,以使其更清晰,但一旦涉及一些逻辑,我回显所有内容,以免一路破坏 php 流程。
但最终,这完全取决于你。
It doesn't matter at all when the file is a php file (and contains php code somewhere). The server executes it regardless of what's inside and pure plain text is returned the way it is and text that is echoed out is added in between (at the echo location). In the end a coherent plain text is produced and sent to the client (with the correct mime-type) which then makes the browser interpret it.
I usually put in longer blocks of static html outside of
<?php .. ?>
tags to make it more clear, but as soon as there is some logic involved, I echo everything out, to not break the php flow all the way.But in the end, it is totally up to you.
我同意亚历克斯的观点。您应该将数据层和显示层分开。一个流行的解决方案是使用像 Smarty (http://www.smarty.net) 这样的模板系统。我们在工作中使用它。有些人喜欢,有些人不喜欢;你可以自己决定。
I agree with Alex. You should keep your data and display layers separate. A popular solution would be to use a template system like Smarty (http://www.smarty.net). We use it at my work. Some people like it and some don't; you can decide that for yourself.
静态 html 比 echo 或变量上的嵌入 html 具有更高的性能,因为 html 不是由服务器端处理的。因此,一个有用的实现是从服务器端仅检索必要的数据变量、数据库行、迭代等,因为内部的所有内容都将由 php 服务处理。
A static html has a higher performance than embedded html on echo or variables 'cause the html isnt proccessed by the server-side. So a useful implementation is to retrieve only necesary data vars, database rows, iterations, etc from the server-side because everything inside is gonna be proccessed by the php service.