在echo文件中输出HTML文件的PHP代码是否更好? [英] Is it allways better to output HTML code in a php file with echo?

查看:69
本文介绍了在echo文件中输出HTML文件的PHP代码是否更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

在PHP中回显HTML的最简单方法?

你好,

一个简单而简短的问题。如果您有一个包含HTML代码的php文件,使用echo执行输出还是直接将HTML代码写入文件最好?例如,some.php文件包含:

one simple and short question. If you have a php file that contains HTML code, is it better to do the output with echo or to write the HTML code directly into the file? For example the file some.php contains either:

<div>This is a text</div><a href="www.example.com">test</a><?php if(---) { whatever; } ?>

或:

or:

<?php echo "<div>This is a text</div><a href=\"www.example.com\">test</a>"; if(---) { whatever; } ?>

哪个版本更快,更干净,更好?

Which version is faster, cleaner and better?

推荐答案

在我看来,这取决于代码段的时间长短。对于简单的线路,我只是使用回声。对于较长的零件(特别是有很多逃离),我更愿意关闭php代码(第二种方法)。

In my opinion this really depends on how long the code snippet is. For simple lines I'd just use echo. For longer parts (especially with lots of " to escape) i'd prefer "closing" the php code (your second approach).

根据文本/要打印的HTML我还会考虑使用某种简单的模板引擎来加载模板文件,并只填写动态变量/占位符/间隙。

Depending on the amount of text/HTML to print I'd also consider using some kind of simple templating engine to load a template file and just fill in dynamic variables/placeholders/gaps.

这篇关于在echo文件中输出HTML文件的PHP代码是否更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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