如何在 PHP 中回显 HTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1100354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I echo HTML in PHP?
提问by TheFlash
I want to conditionally output HTML to generate a page, so what's the easiest way to echo multiline snippets of HTML in PHP 4+? Would I need to use a template framework like Smarty?
我想有条件地输出 HTML 以生成页面,那么在 PHP 4+ 中回显多行 HTML 片段的最简单方法是什么?我需要使用像 Smarty 这样的模板框架吗?
echo '<html>', "\n"; // I'm sure there's a better way!
echo '<head>', "\n";
echo '</head>', "\n";
echo '<body>', "\n";
echo '</body>', "\n";
echo '</html>', "\n";
回答by Chris Bier
There are a few ways to echo HTML in PHP.
有几种方法可以在 PHP 中回显 HTML。
1. In between PHP tags
1.在PHP标签之间
<?php if(condition){ ?>
<!-- HTML here -->
<?php } ?>
2. In an echo
2. 在回声中
if(condition){
echo "HTML here";
}
With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:
使用回显,如果您希望在 HTML 中使用双引号,则必须使用单引号回显,如下所示:
echo '<input type="text">';
Or you can escape them like so:
或者你可以像这样逃避它们:
echo "<input type=\"text\">";
3. Heredocs
3. 继承人
4. Nowdocs (as of PHP 5.3.0)
4. Nowdocs(自 PHP 5.3.0 起)
Template enginesare used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>).
模板引擎用于在主要包含 HTML 的文档中使用 PHP。事实上,PHP 的最初目的是成为一种模板语言。这就是为什么在 PHP 中您可以使用诸如短标签之类的东西来回显变量(例如<?=$someVariable?>)。
There are other template engines (such as Smarty, Twig, etc.) that make the syntax even more concise (e.g. {{someVariable}}).
还有其他模板引擎(例如 Smarty、Twig 等)使语法更加简洁(例如{{someVariable}})。
The primary benefit of using a template engine is keeping the design (Presentation Logic) separate from the coding (Business Logic). It also makes the code cleaner and easier to maintain in the long run.
使用模板引擎的主要好处是将设计(呈现逻辑)与编码(业务逻辑)分开。从长远来看,它还使代码更清晰,更易于维护。
If you have any more questions feel free to leave a comment. Further reading is available on these things in the PHP Documentation.
如果您有更多问题,请随时发表评论。在PHP 文档中可以进一步阅读这些内容。
God Bless!
上帝保佑!
NOTE:PHP short tags <?and ?>are discouraged because they are only available if enabled with short_open_tagphp.ini configuration file directive, or if PHP was configured with the --enable-short-tagsoption. They are available, regardless of settings from 5.4 onwards.
注意:PHP 短标签<?并且?>不鼓励使用,因为它们仅在使用short_open_tagphp.ini 配置文件指令启用时才可用,或者如果 PHP 使用该--enable-short-tags选项进行配置。无论 5.4 以后的设置如何,它们都可用。
回答by lfx
try like this:
试试这样:
$variable = <<<XYZ
<html>
<body>
</body>
</html>
XYZ;
echo $variable;
回答by Tom Haigh
You could use the alternative syntax alternative syntax for control structuresand break out of php:
您可以使用替代语法替代语法来控制结构并突破 php:
<?php if ($something): ?>
<some /> <tags /> <etc />
<?=$shortButControversialWayOfPrintingAVariable ?>
<?php /* A comment not visible in the HTML but is a bit of a pain to write */ ?>
<?php else: ?>
<!-- else -->
<?php endif; ?>
回答by DisgruntledGoat
Basically you can put HTML anywhere outside of PHP tags. It's also very beneficial to do all your necessary data processing before displaying any data, in order to separate logic and presentation.
基本上你可以把 HTML 放在 PHP 标签之外的任何地方。在显示任何数据之前进行所有必要的数据处理也是非常有益的,以便分离逻辑和表示。
The data display itself could be at the bottom of the same PHP file or you could include a separate PHP file consisting of mostly HTML.
数据显示本身可以位于同一个 PHP 文件的底部,或者您可以包含一个主要由 HTML 组成的单独 PHP 文件。
I prefer this compact style:
我更喜欢这种紧凑的风格:
<?php
/* do your processing here */
?>
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<?php foreach ( $something as $item ) : ?>
<p><?=$item?></p>
<?php endforeach; ?>
</body>
</html>
Note: you may need to use <?php echo $var; ?>instead of <?=$var?>depending on your PHP setup.
注意:您可能需要使用<?php echo $var; ?>而不是<?=$var?>取决于您的 PHP 设置。
回答by julz
Another approach is put the HTML in a separate file and mark the area to change with a placeholder [[content]] in this case. (You can also use sprintf instead of the str_replace.)
另一种方法是将 HTML 放在一个单独的文件中,并在这种情况下用占位符 [[content]] 标记要更改的区域。(您也可以使用 sprintf 代替 str_replace。)
$page = 'hello world';
$content = file_get_contents('html/welcome.html');
$pagecontent = str_replace('[[content]]',$content,$page);
echo($pagecontent);
Alternatively you can just output all the php stuff to the screen captured in a buffer, then write the html, then put the php output back into the page.
或者,您可以将所有 php 内容输出到缓冲区中捕获的屏幕,然后编写 html,然后将 php 输出放回页面。
It might seem strange to write the php out, catch it, then write it again, but it does mean that you can do all kinds of formatting stuff (Heredocs etc),& test it outputs correctly without the hassle of the page template getting in the way. (Joomla CMS does it this way, BTW) ie:
把 php 写出来,抓住它,然后再写一遍,这似乎很奇怪,但这确实意味着你可以做各种格式化的东西(Heredocs 等),并测试它的输出是否正确,而不会出现页面模板的麻烦道路。(Joomla CMS 就是这样做的,顺便说一句)即:
<?php
ob_start();
echo('hello world');
$php_output = ob_get_contents();
ob_end_clean();
?>
<h1> My Template page says </h1>
<?php
echo($php_output );
?>
<hr>
template footer
回答by John Kugelman
I am partial to this style:
我偏爱这种风格:
<html>
<head>
<% if (X)
{
%> <title>Definitely X</title>
<% }
else
{
%> <title>Totally not X</title>
<% }
%> </head>
</html>
I do use ASP-style tags, yes. The blending of PHP and HTML looks super-readable to my eyes. The trick is in getting the <%and %>markers just right.
我确实使用 ASP 样式的标签,是的。PHP 和 HTML 的混合在我看来超级可读。诀窍是让<%和%>标记恰到好处。
回答by ezequiel wernicke
$enter_string = '<textarea style="color:#FF0000;" name="message">EXAMPLE</textarea>';
echo( 'Echo as HTML' . htmlspecialchars( (string)$enter_string ) );
回答by Code Spy
Simply use printfunction to echo text in PHP file as follow
只需使用打印功能在 PHP 文件中回显文本如下
<?php
print('
<div class="wrap">
<span class="textClass">TESTING</span>
</div>
')
?>
回答by Alessandro
echo '
<html>
<body>
</body>
</html>
';
or
或者
echo "<html>\n<body>\n</body>\n</html>\n";
回答by edCoder
Try This May Help You.......
试试这个可能对你有帮助......
<?php
echo <<<HTML
your html tags here
HTML;
?>

