dpdhf02040 2014-03-23 15:49
浏览 29
已采纳

什么是更快:将html附加到数组或使用混合的html / php

What is the faster way to output html in PHP that contains PHP variables?

Version A:

<div id="example"><?php echo $var; ?></div>
... much more like this

or

Version B:

$html = array();

$html[] = '<div id ="example">';
$html[] = &$var;
$html[] = '</div>';
$html[] = '... much more like this';

echo implode( '', $html );
  • 写回答

2条回答 默认 最新

  • dsd30433 2014-03-23 15:52
    关注

    In this case it is faster to mix html and php. Version B, will essentially hold a big array in memory, and in the end loop through all the elements. So it will be slower, than just print/echo the data right away. Either way, I won't recommend either approach. I will suggest using a template engine and provide it with data, instead of mixing php and html, this way you can keep markup and logic completely separate. This is often done as a part of a MVC pattern as @Wrong described.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了