gpt4 book ai didi

php - 将 HTML 转义为 PHP 还是使用 Echo?哪个更好?

转载 作者:IT王子 更新时间:2023-10-29 00:54:01 25 4
gpt4 key购买 nike

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

我知道可能存在一些可读性问题,但我不担心。

谢谢大家!

示例 1

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

<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 中完成。

例如:

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

对比:

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

或者

<?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
);
?>

关于php - 将 HTML 转义为 PHP 还是使用 Echo?哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/505642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com