在 PHP 中使用常规 HTML 和在 echo 语句或变量中使用 HTML 之间的区别

发布于 2024-09-28 03:41:19 字数 334 浏览 9 评论 0原文

你好,我一直在思考哪种是在 MVC 架构中编写 html 的最佳方式。它会是常规的 html

吗?
echo '
<输入类型=“文本”名称=“tBox”/>
';
。这是一个非常小的示例,但有时我们可以使用具有多行代码的单个变量或单个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫多说 2024-10-05 03:41:19

常规的直接 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.

爱她像谁 2024-10-05 03:41:19

当文件是 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.

凡尘雨 2024-10-05 03:41:19

我同意亚历克斯的观点。您应该将数据层和显示层分开。一个流行的解决方案是使用像 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.

何必那么矫情 2024-10-05 03:41:19

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文