PHP 中包含和不包含 HTML 之间有什么区别?
下面两段代码有什么区别?版本 B 似乎更难阅读,过度使用开始和结束标签,但我发现很多教程和示例都使用这种格式。是否存在一种首选方法,即一种方法相对于另一种方法的程序原因,或者这是个人偏好?
版本 A(包含在 php 中)
<?php
$test = array('a','b','c');
if (isset($test))
{
echo '<div id="testmessage">
<h2>
Test Message Below
</h2>
<ul>';
foreach ($test as $t)
{
echo '<li>'.$t.'</li>';
}
echo '</ul>';
echo '</div>';
}
?>
版本 B(多个开始和结束标签)
<?php $test = array('a','b','c');
if (isset($test)){
?>
<div id="testmessage">
<h2>
Test Message Below
</h2>
<ul>
<?php
foreach ($test as $t)
{
?>
<li><?php echo $t; ?></li>
<?php
}
?>
</ul>
</div>
<?php
}
?>
What's the difference between the following two pieces of code? Version B seems harder to read, with excessive use of open and closing tags, but I find a lot of tutorials and examples using this format. Is there a preferred approach, meaning a procedural reason for one over the other, or is this personal preference?
Version A (enclosed in php)
<?php
$test = array('a','b','c');
if (isset($test))
{
echo '<div id="testmessage">
<h2>
Test Message Below
</h2>
<ul>';
foreach ($test as $t)
{
echo '<li>'.$t.'</li>';
}
echo '</ul>';
echo '</div>';
}
?>
Version B (Multiple open and closing tags)
<?php $test = array('a','b','c');
if (isset($test)){
?>
<div id="testmessage">
<h2>
Test Message Below
</h2>
<ul>
<?php
foreach ($test as $t)
{
?>
<li><?php echo $t; ?></li>
<?php
}
?>
</ul>
</div>
<?php
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有真正的区别,但只是不鼓励这样做。
我给你版本 C,它可能比 B 读起来好一点:
No real difference, but it is just not encouraged.
And I give you version C which might read a little better than B:
它实际上只是一种风格,但我更喜欢 B 与模板引擎的结合。
It's really just a style-thing, however I prefer B in combination with a template engine.
虽然您可能认为 B 更难阅读,但实际上它更容易维护。您所拥有的内容可以粗略地描述为模板。
当您开始在 PHP 中嵌入 HTML(如版本 A)时,如果稍后需要更改页面的外观,会发生什么情况?或者当您想更改页面布局时会发生什么?
通过将 CSS、javascipt/jquery 等技术与 html 相结合,管理网站页眉、页脚和主要内容块变得更有条理。
我认为从长远来看,您会发现使用模板会让您的生活更轻松。在您认为需要自己包装一个之前,有许多模板引擎可供使用从中选择。
While you might think that B is harder to read, it is actually easier to maintain. What you have there can be loosely described as a template.
When you start to embed HTML like version A in PHP, what happens if you need to change the look of your page later on? or what happens when you want to change the layout of your page?
By combining technologies like CSS, javascipt/jquery with your html, managing your site headers, footers and main content blocks becomes more organized.
I think you'll find using templates will make your life easier in the long run. Before you go off and think you need to wrap one on your own, there are many template engines to choose from.
除了 PHP 用于存储这些 HTML 字符串的内存之外什么都没有。通过不在 PHP 脚本中包含 HTML,您最终会减小 PHP 解析器的大小(和压力),因为它读取的 PHP 脚本最终会更小。
B 版更好,但也只是好一点。在大多数情况下,您不会注意到其中的差异。
Nothing, except the memory that PHP uses in storing those HTML strings. By not including the HTML in the PHP scripts you're ultimately reducing the size (and strain) of the PHP parser, because the PHP script that it reads will ultimately be smaller.
Version B is better, but only slightly. In most cases, you won't notice the difference.
没有实际的区别——只是风格问题。
您可能更喜欢第二种方式,因为您不需要在 HTML 中转义引号等。
There's no practical difference -- just a matter of style.
You might prefer the second way because you don't need to escape quotation marks, etc. in the HTML.