PHP 中包含和不包含 HTML 之间有什么区别?

发布于 2025-01-03 04:35:38 字数 1040 浏览 0 评论 0原文

下面两段代码有什么区别?版本 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 技术交流群。

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

发布评论

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

评论(5

掩饰不了的爱 2025-01-10 04:35:39

没有真正的区别,但只是不鼓励这样做。

我给你版本 C,它可能比 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 endforeach;    ?>

        </ul>
    </div>

<?php endif; ?>

No real difference, but it is just not encouraged.

And I give you version C which might read a little better than 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 endforeach;    ?>

        </ul>
    </div>

<?php endif; ?>
守不住的情 2025-01-10 04:35:39
  • A 看起来像组合 PHP 和 HTML 的标准 hack-away 风格
  • B 看起来像本机 PHP 模板引擎(例如 Zend View)的模板

它实际上只是一种风格,但我更喜欢 B 与模板引擎的结合。

  • A looks like the standard hack-away style of combining PHP and HTML
  • B looks like a template for a native PHP template engine (e.g. Zend View)

It's really just a style-thing, however I prefer B in combination with a template engine.

药祭#氼 2025-01-10 04:35:39

虽然您可能认为 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.

肩上的翅膀 2025-01-10 04:35:39

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

鹊巢 2025-01-10 04:35:39

没有实际的区别——只是风格问题。

您可能更喜欢第二种方式,因为您不需要在 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.

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