将HTML转换为PHP或使用Echo?哪个更好? [英] Escape HTML to PHP or Use Echo? Which is better?

查看:98
本文介绍了将HTML转换为PHP或使用Echo?哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在性能方面,会更好。使用PHP来回显所有的HTML输出,这样我就可以使用各种工作代码和变量,或者在整个文档中定期将HTML转换为PHP。

In terms of performance , what would be better. Using PHP to echo all the HTML output so I can pepper it with the various bits of working code and variables or escape HTML to php periodically throughout the documents.

我知道那里可能是一些可读性问题,但我不担心这一点。

I know there may be some readability issues but I'm not to worried about that.

感谢所有!

示例1

Example 1

echo '<html>',
     '<body>',
     'The content of the ',$container,' element is displayed in your ', $other_container,
     '</body>',
     '</html>';

OR

<html>
<body>
The content of the <?php echo $container; ?> element is displayed in your <?php echo $other_container; ?>
</body>
</html>


推荐答案

当然,这会随着每种情况而变化。如果你正在做整个页面,并且有很大的部分没有任何PHP,那么我会跳出PHP并且只写纯HTML,而如果有一个部分有很多PHP变量,我会在PHP中完成。

It's all about which you find the most readable. Of course, this will vary with each situation. If you were doing an entire page, and there were large sections which did not have any PHP in it, then I'd break out of PHP and just write the plain HTML, whereas if there was a section which had a lot of PHP variables, I'd do it all in PHP.

例如:

For example:

<table>
    <tr>
        <td colspan="<?php echo $numCols; ?>">
            <?php echo $a; ?>, <?php echo $b; ?>, and <?php echo $c?>
        </td>
    </tr>
</table>

与:

versus:

<?php
echo "<table>"
    . "<tr>"
    .    "<td colspan=\"" . $numCols . "\">"
    .        $a . ", " . $b . " and " . $c
    .    "</td>"
    . "</tr>"
    . "</table>"
; ?>



Or

<?php
echo "<table>
         <tr>
            <td colspan='{$numCols}'>
               {$a}, {$b}, and {$c}
            </td>
         </tr>
      </table>";
?>

另外不要忘记 printf

<?php
printf("<table>"
    . "<tr>"
    .    "<td colspan=\"%d\">%s, %s and %s</td>"
    . "</tr>"
    . "</table>"
    , $numCols
    , $a
    , $b
    , $c
);
?>

这篇关于将HTML转换为PHP或使用Echo?哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆